Skip to content

Commit

Permalink
fixed class model has no attribute id
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinktah committed Sep 7, 2023
1 parent eb18ce6 commit e0be900
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,10 @@ def create(
dictionary, query=True, id_key=cls.__primary_key__, cast=cast, **kwargs
).to_sql()

# when 'id_key' is already present in kwargs, the previous version raised
kwargs['id_key'] = cls.__primary_key__
return cls.builder.create(
dictionary, id_key=cls.__primary_key__, cast=cast, **kwargs
dictionary, cast=cast, **kwargs
)

@classmethod
Expand Down Expand Up @@ -711,9 +713,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)
return self.where(wheres).first()

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

0 comments on commit e0be900

Please sign in to comment.