-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Conversation
There was a problem hiding this 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.
for k, v in self.model_attrs.items(): | ||
if is_iterator(v): | ||
self.model_attrs[k] = next(v) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Please, consider my comment in the issue card you've created. Maybe the way to go is another one. |
Closing this PR due to inactivity and lack of response from the PR creator. |
fixes #204
next
was not being called on iterator that holds related objects