Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed class model has no attribute id when using update_or_create #855

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,19 @@ def update_or_create(cls, wheres, updates):
total.update(updates)
total.update(wheres)
if not record:
return self.create(total, id_key=cls.get_primary_key())

return self.where(wheres).update(total)
# if we don't return fresh, we don't get the primary_key that has been used,
# and we can't call it from outside the function lest we get a QueryBuilder.
#
# Without this we are reduced to performing a DIY update_or_create, e.g.:
# ebay_order = EbayOrder.where({'order_id': d['order_id']}).first()
# if not ebay_order:
# ebay_order = EbayOrder.create(d).fresh()
# else:
# ebay_order.save()
return self.create(total, id_key=cls.get_primary_key()).fresh()

rv = self.where(wheres).update(total)
josephmancuso marked this conversation as resolved.
Show resolved Hide resolved
return self.where(wheres).first()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfinktah i'm actually reverting this change. the wheres could not be unique enough i think for this to work. its possible we could do

return self.where(total).first()

but it would have to be tested


def relations_to_dict(self):
"""Converts a models relationships to a dictionary
Expand Down
Loading