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

Conversation

sfinktah
Copy link
Contributor

@sfinktah sfinktah commented Sep 7, 2023

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

Copy link
Member

@josephmancuso josephmancuso left a comment

Choose a reason for hiding this comment

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

i think this looks right

src/masoniteorm/models/Model.py Show resolved Hide resolved
@yubarajshrestha
Copy link
Contributor

@sfinktah fix the linter issues.

@josephmancuso josephmancuso merged commit c01e632 into MasoniteFramework:2.0 Dec 6, 2023
4 of 5 checks passed
@josephmancuso josephmancuso changed the title fixed class model has no attribute id fixed class model has no attribute id when using update_or_create Dec 6, 2023
josephmancuso added a commit that referenced this pull request Dec 6, 2023
return self.create(total, id_key=cls.get_primary_key()).fresh()

rv = self.where(wheres).update(total)
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants