0

我在数据库中有三个表。
任务表是

idTask 任务 后代ID
1 1+2+3+4 4
2 6+7+8+9 4

类别表是

id_c 名称类别
1 数学
2 代数
3 进步
4 编号规则

Closure_table_category 是

祖先 后裔 深度
1 1 0
1 2 1
1 3 2
1 4 3
2 2 0
2 3 1
2 4 2
3 3 0
3 4 1
4 4 0

我需要在 Flask 中上课。我试着写:

db = SQLAlchemy(app)
migrate = Migrate(app, db)

Closure_table_category = db.Table('Closure_table_category',
                                  db.Column('ancestor', db.Integer, db.ForeignKey('topics.id')),
                                  db.Column('descendant', db.Integer, db.ForeignKey('topics.id')),
                                  db.Column('depth', db.Integer, nullable=False)
                                  )


class Tasks(db.Model):
    __tablename__ = 'tasks'

    id        = db.Column(db.Integer, primary_key=True, autoincrement=True)
    task      = db.Column(db.Text)

    topics = db.relationship('Topics')
    topics_id = db.Column(db.Integer, db.ForeignKey('topics.id'))


class Topics(db.Model):
    __tablename__ = 'topics'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(140))

    descendant = db.relationship("Tasks",
                                 secondary=Closure_table_category,
                                 back_populates='topics')

我得到一个错误:

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship Topics.descendant - there are no foreign keys linking these tables via secondary
table 'Closure_table_category'.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify 'primaryjoin' and 'secondaryjoin' 

我应该如何解决这个问题?

4

0 回答 0