Post
というモデルがあり、これにはis_answer
というブール値フィールドがあります。 のis_answer
フィールドがTrueの場合、それは「質問」です。そうでなければ、それは "答え"です。Flask-SQLAlchemyで自己参照型の一対多関係を作成するには?
「質問」には「回答」が1つ多くありますが、「回答」には1つの「質問」しかありません。 「質問」と「回答」の両方が本質的にPost
であるという事実のために、私はその関係が自己参照でなければならないと考えています。ここで
は、私が試したものです:
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
is_question = db.Column(db.Boolean)
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
question = db.relationship('Post', backref=db.backref('answer', lazy='dynamic'), uselist=False, lazy='dynamic')
エラーは次のとおりです。
ArgumentError: Post.question and back-reference Post.answer are both of the same direction symbol('ONETOMANY'). Did you mean to set remote_side on the many-to-one side ?