From 3c5599adf7fabee1919da5a5b8acdcb524840fc5 Mon Sep 17 00:00:00 2001 From: Lephe Date: Wed, 7 Jul 2021 19:38:28 +0200 Subject: [PATCH] model: avoid the foreign key cycle between Thread and Comment By specifying use_alter=True on one of the keys, the ORM will generate an ALTER command to set the second foreign key after inserting both objects, in case two new objects referring to each other have to be created. See: https://docs.sqlalchemy.org/en/14/core/exceptions.html#sqlalchemy.exc.CircularDependencyError --- app/models/thread.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/thread.py b/app/models/thread.py index 418c6bc..8dfaa52 100644 --- a/app/models/thread.py +++ b/app/models/thread.py @@ -9,7 +9,8 @@ class Thread(db.Model): id = db.Column(db.Integer, primary_key=True) # Top comment - top_comment_id = db.Column(db.Integer, db.ForeignKey('comment.id')) + top_comment_id = db.Column(db.Integer, + db.ForeignKey('comment.id', use_alter=True)) top_comment = db.relationship('Comment', foreign_keys=top_comment_id) # Post owning the thread, set only by Topic, Program, etc. In general, you