model-bakers / model_bakery

Object factory for Django
https://model-bakery.readthedocs.io/en/latest/
Other
846 stars 85 forks source link

Reverse one-to-one relation not persisting in database #473

Open AlmerCarbonEquity opened 9 months ago

AlmerCarbonEquity commented 9 months ago

I have the following model:

class Person(models.Model):
    user = models.OneToOneField("auth.User", on_delete=models.CASCADE, blank=True, null=True)

Now I want to make a recipe for a user that has a person to use in tests:

user = Recipe(User)
person = Recipe(Person)
user_with_person = user.extend(person=foreign_key(person))

when I check test_user = user_with_person.make() it has a test_user.person but when I check the database like this: User.objects.get(id=test_user.id).person it doesn't exist. I can create a person_with_user just fine. Is it not possible to define a reverse relation like this? I've made a small django project to showcase the issue: https://github.com/AlmerCarbonEquity/model-bakery-one-to-one

Expected behavior I would expect User.objects.get(id=test_user.id).person to exist after test_user = user_with_person.make()

Versions

kashyl commented 6 days ago

Currently having the same issue @AlmerCarbonEquity was wondering if you found any workaround?