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

Fix recipes with foreign_key and one_to_one=True #205

Closed
wants to merge 3 commits into from

Conversation

walison17
Copy link

fixes #204

next was not being called on iterator that holds related objects

Copy link
Member

@berinhard berinhard left a comment

Choose a reason for hiding this comment

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

Hi @walison17 thanks for opening this PR, but I don't thing what you're proposing here is possible. I mean, to create 1-1 relationships via related name, will always result in the double of objects for one of the 2 sides of the FK.

Also, the fix you suggests, although it works, it has a collateral effect of writing more data in the database from what you've probably expected.

Comment on lines +370 to +372
for k, v in self.model_attrs.items():
if is_iterator(v):
self.model_attrs[k] = next(v)
Copy link
Member

Choose a reason for hiding this comment

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

This is not ideal because it's bypassing all of bakery's default behavior. The proper way to do so is to make sure that self.iterator_attrs has the iterator object and the model_attrs doesn't.

From what I saw, probably the issue is under the self.get_fields methods because it's not listing the related name as a possible field for the User model.

def test_reverse_one_to_one_relationship(self):
users = baker.make_recipe("tests.generic.user_with_profile", _quantity=2)
profile_ids = {u.another_profile.id for u in users}
assert len(profile_ids) == 2
Copy link
Member

Choose a reason for hiding this comment

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

You can check here by the number of users in the database and you'll see that there are 4 of then, while you're probably expecting to only have 2.

@berinhard
Copy link
Member

Please, consider my comment in the issue card you've created. Maybe the way to go is another one.

@berinhard
Copy link
Member

Closing this PR due to inactivity and lack of response from the PR creator.

@berinhard berinhard closed this Mar 31, 2022
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.

Recipes with foreign_key and one_to_one=True breaks
2 participants