I have a model with a column which is a self referential foreign key.
class Foo(db.Model):
foo_ref_id = db.Column(
db.Integer,
db.ForeignKey("foo.id", ondelete="CASCADE"),
nullable=True,
index=True,
)
I am trying to create a factory model for the same model:
class FooFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Foo
context_base_id = factory.SubFactory("tests.factories.BaseFactory", parent=None)
context_base_id = factory.LazyAttribute(lambda x: BaseFactory(parent=None))
context_base_id = factory.Trait(
parent_category=factory.SubFactory("tests.factories.BaseFactory")
)
I have tried the 3 ways of doing achieving this. All of them return an error of maximum depth of recursion exceeded.
What is the proper way of doing this?
You have to tell the factory where to stop.
The simplest option is to pass an extra parameter to the
SubFactorycall:With the code above:
FooFactorywill setself.parent = FooFactory(parent__parent__parent=None)self.parent = FooFactory(parent__parent=None)self.parent = FooFactory(parent=None)self.parent = None, thus ending the recursion.