我有一张桌子
class Table1(db.Model):
id = db.Column(db.Integer, primary_key=True)
primary_category_id = db.Column(db.Integer)
def __repr__(self):
return '<Table1 %r>' % (self.id)
已经填充了。现在我想添加一个新表
^{pr2}$而旧表Table1的category_id现在应该是新表的外键。我使用flask migrate来添加Query_Category表,这很好,但是如何添加外键约束呢? 我试着把它加上
primary_category_id = db.Column(db.Integer, db.ForeignKey('query_category.category_id'))
运行flask migrate,但这会导致以下错误
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'Table1.primary_category_id' could not find table 'query_category' with which to generate a foreign key to target column 'category_id'
如何向现有表添加外键约束?在
尝试向模型中添加
__tablename__
字段相关问题 更多 >
编程相关推荐