[Fixed]-One to One relationship in factory – Integrity Error

0👍

The problem was I had another model with another one to one to another class that inherited from Test.

I added the subfactory to the factory of this other class and the problem was solved.

1👍

test = factory.RelatedFactory(PlaylistTestFactory)

You need to use a SubFactory rather than a RelatedFactory so that it creates the test object first:

A RelatedFactory behaves mostly like a SubFactory, with the main
difference that the related Factory will be generated after the
base Factory.

https://factoryboy.readthedocs.org/en/latest/reference.html#factory.RelatedFactory

👤Oin

Leave a comment