model_bakery
model_bakery copied to clipboard
Reverse one-to-one relation not persisting in database
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
- Python: [3.12.0]
- Django [5.0.2]
- Model Bakery [1.17.0]