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
This commit is contained in:
Lephe 2021-07-07 19:38:28 +02:00
parent 3876e3470e
commit 3c5599adf7
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
1 changed files with 2 additions and 1 deletions

View File

@ -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