微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlalchemy.exc.NoForeignKeysError 在 sqlalchemy 中制作闭包表

如何解决sqlalchemy.exc.NoForeignKeysError 在 sqlalchemy 中制作闭包表

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

idTask 任务 descendant_id
1 1+2+3+4 4
2 6+7+8+9 4

类别表是

id_c name_category
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.Column('depth',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,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' 

我该如何解决这个问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。