-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
_bulk_create=True silently and undocumentedly does not create M2M-entries #298
Comments
Hi @he3lixxx thanks for opening this issue and you're correct, it has been fixed with the #206 PR that also fixed #202. I'm releasing a new version with the fix. By the way, here's how I tested it based on your models: def test_sample(self):
with self.assertNumQueries(2):
house = baker.make(models.House)
details = baker.make(models.HouseDetail, houses=[house], _quantity=20, _bulk_create=True)
assert models.House.objects.count() == 1
assert models.HouseDetail.objects.count() == 20
d1, d2 = models.HouseDetail.objects.all()[:2]
assert list(d1.houses.all()) == list(d2.houses.all()) Important: models created with |
I'm sorry, but I'm a bit confused here.
The code snipped you posted asserts that assert list(d1.houses.all()) == list(d2.houses.all()) which would also be true if assert d1.houses.count() == 1
#206 was merged in June 2021 and was part of 1.3.3, so if this was fixed in #206, there shouldn't be the need for a new release and I shouldn't be able to make this fail with 1.4.0, right? |
My bad here @he3lixxx. You're correct about the issue, it's still happening. I changed the assert to assert list(d1.houses.all()) == list(d2.houses.all()) == [house] And I got the following traceback:
I'm reopening this issue. |
Hey everyone, I created a fix for this issue here: #354 However, I got hit by something else while running the test on Django 3.2 (Django 4.0 works just fine). @he3lixxx if you are still interested in this bug and can test it for your systems, feel free to try out branch version: Best, |
Ok, found, the difference in Django 3.2 and Django 4.0 that affects SQLite tests: |
Hey @he3lixxx ! Sorry, it took us long, but we released the fix in model-bakery 1.9.0. I'd love to get your feedback on how this works for you 🙏 |
Hey @amureki thanks for putting time into this. The forward-case is indeed solved with 1.9.0. However, when referring to the many2many relationship using its reverse_name, the relationship still is not created. Using the example from the initial post, the first block passes, but the second one fails: house = baker.make(House)
details = baker.make(HouseDetail, houses=[house], _quantity=20, _bulk_create=True)
assert details[0].houses.count() == 1 # passes since 1.9.0
###
detail = baker.make(HouseDetail)
houses = baker.make(House, housedetail_set=[detail], _quantity=20, _bulk_create=True)
assert houses[0].housedetail_set.count() == 1 # fails Again, when not using bulk_create, the relationship is created as expected. Should I open a new issue for this? |
@he3lixxx ohh, good point, that one was missed. I'd need to dig into the reverse relations and how to cover them as well. |
Probably related to #202, as it is the same issue applied to ManyToManyField.
When using
baker.make
with_bulk_create=True
, it will silently ignore arguments that should be used to fill many-to-many fields.Expected behavior
with
I'd expect
to work.
If it can't, for some technical reason, I'd expect it to be documented that this is not supported, and raise an exception.
Actual behavior
Currently, this code only raises at the assertion, and I couldn't find any remarks in the documentation.
If the
_bulk_create=True
argument is removed, everything behaves as expected.Versions
Python: 3.7.5
Django: 3.2
Model Bakery: 1.4.0
The text was updated successfully, but these errors were encountered: