(0.16.14) Cyclic FK References
See original GitHub issueI have 2 models: User and FileAttachment.
User is John Doe User uploads a random file, he becomes the owner of that FileAttachment using a ‘created_by’ field on the FileAttachment I try to add a ‘avatar’ field on the User object that’s a foreignkey reference to FileAttachment, I get the cyclic fk error.
class User(Model):
first_name = fields.CharField(max_length=255, null=False)
last_name = fields.CharField(max_length=255, null=False)
picture = fields.ForeignKeyField('models.FileAttachment')
class FileAttachment(Model):
created_by = fields.ForeignKeyField('models.User', related_name='files')
File "py", line 46, in init_tortoise
await Tortoise.generate_schemas()
File "/Users/phil/.pyenv/versions/lib/python3.8/site-packages/tortoise/__init__.py", line 596, in generate_schemas
await generate_schema_for_client(connection, safe)
File "/Users/phil/.pyenv/versions/lib/python3.8/site-packages/tortoise/utils.py", line 29, in generate_schema_for_client
schema = get_schema_sql(client, safe)
File "/Users/phil/.pyenv/versions/lib/python3.8/site-packages/tortoise/utils.py", line 18, in get_schema_sql
return generator.get_create_schema_sql(safe)
File "/Users/phil/.pyenv/versions/lib/python3.8/site-packages/tortoise/backends/base/schema_generator.py", line 408, in get_create_schema_sql
raise ConfigurationError("Can't create schema due to cyclic fk references")
tortoise.exceptions.ConfigurationError: Can't create schema due to cyclic fk references
If I make the changes directly in Postgres, it accepts them. It’s only tortoise that seems to dislike it 😃
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Changelog - Tortoise ORM v0.19.3 Documentation
Fix ForeignKeyField is none when fk is integer 0. (#1274). Fix limit ignore zero. (#1270) ... (#335). Remove aerich in case of cyclic...
Read more >tortoise/community - Gitter
Thing is : i have a cyclic reference between my tables. System has a fk fronter which references Member fronter = fields.ForeignKeyField("models.
Read more >How to deal with a cyclic foreign key constraint using ...
In simple terms, a cyclic foreign key can be closely related to circular key references. Let's assume we have the following two tables:...
Read more >Foreign Key cyclic reference Dilemma - mysql - Stack Overflow
You need to allow at least one of the foreign keys to be NULL . This will allow you to create the first...
Read more >Is it acceptable to have circular foreign key references\How to ...
No, it's not acceptable to have circular foreign key references. Not only because it would be impossible to insert data without constantly dropping...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Oh sorry I missed the thread here. I don’t think it’s a question of good or bad model design. Sounds more like a software limitation 😛 A user field on each model for
created_by
is pretty standard. To follow this logic I would have to create a new table/model for avatars instead of using my File object, which is what I ended up doing but It felt a big wrong to just duplicate objects to counter the limitation.I also meet this problem when I’m doing migration using aerich (It’ll use the tortoise generate schema to init db).
My solution is to remove Tortoise.generate_schemas() to make sure the FastAPI can works. For the migration, I temporarily remove the cyclic FKs on one model to make sure the aerich can init-db successful, then add the cyclic FKs back manually to generate the FKs.