MasoniteFramework / orm

Masonite ORM is a beautiful Python ORM. It's also a nearly drop in replacement of the Orator ORM
https://orm.masoniteproject.com
MIT License
160 stars 47 forks source link

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

Closed sfinktah closed 7 months ago

sfinktah commented 10 months ago

The following code is otherwise unworkable:

ebay_transaction = EbayOrderTransaction.update_or_create({'transaction_id': d['transaction_id']}, d)
ebay_order.attach('transactions', ebay_transaction)

as the primary id key is not returned, and one cannot call fresh on the result. it is possible [but undesirable] to work around this in such a way as:

ebay_order = EbayOrder.where({'order_id': d['order_id']}).first()
if not ebay_order:
    ebay_order = EbayOrder.create(d).fresh()
else:
    ebay_order.save()

this PR also fixes an issue (that no doubt occurs in many other places too) were

        return cls.builder.create(          return cls.builder.create(
            dictionary, id_key=cls.__primary_key__, cast=cast, **kwargs             dictionary, cast=cast, **kwargs
        )

will raise an Exception as the library is already passing id_key via kwargs

yubarajshrestha commented 7 months ago

@sfinktah fix the linter issues.