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

baker.make() with _bulk_create=True doesn't create m2m entries on foreign-key related objects #490

Open
richardebeling opened this issue Aug 12, 2024 · 0 comments

Comments

@richardebeling
Copy link

Follow-Up to #385, which was fixed in #486. When specifying values for a M2M field inside an object that is itself accessed via a foreign key, baker doesn't create m2m entries when called with _bulk_create=True.

Inside the test suite, with the additional model definition (so we can have a model with a foreign key field to a model with a m2m field -- none of the existing models currently fulfills this condition)

class HomeOwner(models.Model):
    home = models.ForeignKey(Home, on_delete=models.CASCADE)

put this test in class TestCreateM2MWhenBulkCreate:

def test_create_through_foreign_key_field(self):
    dog = baker.make(models.Dog)
    baker.make(models.HomeOwner, home__dogs=[dog], _quantity=10, _bulk_create=True)

    h1, h2 = models.HomeOwner.objects.all()[:2]
    assert list(h1.home.dogs.all()) == list(h2.home.dogs.all()) == [dog]

It fails with the error:

______ TestCreateM2MWhenBulkCreate.test_create_through_foreign_key_field ______
tests/test_baker.py:1101: in test_create_through_foreign_key_field
    assert list(h1.home.dogs.all()) == list(h2.home.dogs.all()) == [dog]
E   assert [] == [<Dog: Dog object (1)>]
E     
E     Right contains one more item: <Dog: Dog object (1)>
E     
E     Full diff:
E     + []
E     - [
E     -     <Dog: Dog object (1)>,
E     - ]

Versions

  • Python: 3.10.12
  • Django: 4.2
  • Model Bakery 1.19.1 (tested at d758c7a)
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

No branches or pull requests

1 participant