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

Subfactory does not work with self-referencing ForeignKey relationships #94

Open
biletboh opened this issue Aug 24, 2020 · 1 comment
Open

Comments

@biletboh
Copy link

biletboh commented Aug 24, 2020

It seems to be that SubFactory does not work with self-referencing ForeignKey relationships. I have a model with a foreign key to self:

class MyAuthor(Model):
    full_name = CharField('Full name', max_length=255)
    original_author = ForeignKey('self', null=True)

This is the factory I tried to use:

class MyAuthorFactory(factory.django.DjangoModelFactory):
    full_name = 'John Doe'
    original_author = factory.SubFactory('app.factories.MyAuthorFactory')

    class Meta:
        model = 'app.models.Author'

I registered the factory like this:

register(MyAuthorFactory, 'myauthor')

And got this error:

recursive dependency involving fixture 'myauthor' detected
available fixtures: ... myauthor, myauthor__full_name, ...
@MRigal
Copy link

MRigal commented Jun 23, 2021

True. Try using a related_name in the model and the following in the factory:
factory.RelatedFactory('app.factories.MyAuthorFactory', factory_related_name='parent')

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

2 participants