flask-migrate不起作用当我使用ForeignKey添加模型时 [英] flask-migrate doesn't work When I add models with ForeignKey
问题描述
class User(db.Model):
__tablename__ ='user'
id = db.Column(db.Integer,primary_key = True)
email = db.Column(db.String(64),unique = True)
#是不是应该加密下,不能明文存储?应该设置多长的空间?14.7.18 4:22 by lee
password = db.Column(db.String(100))
nickname = db.Column(db.String(64))
school = db.Column(db.String(20))$ (db.String(5))
status = db.Column(db.String(10))
grade = db.Column(db.String(18))
我有一个数据库依然存在。然后我将models添加到models.py中:
$ b $ pre $ class $ PubSquare(db.Model)
id = db.Column (db.Integer,primary_key = True)
$ b $ author_id = db.Column(db.Integer,db.ForeignKey('user.id'))
author = db.relationship('User ',backref = db.backref('publish'))
subject = db.Column(db.String(100))
timestamp = db.Column(db.DateTime,default = datetime.datetime.now)
然后我运行migrate脚本,它调用bug:
证明我的模型代码是正确的,我重新创建数据库,它的工作原理。 从我可以看到<$ c没有创建$ c> user NoReferencedTableError:与'pub_square.author_id'列关联的外键找不到表'user',用于生成一个外键以将列'id' code
表(
这个时候,我可以成功地运行脚本,但是这次当引用foreignkey关系时,它不起作用。
所以,这是瓶颈迁移到这个bug。无法找到表'user',用它来产生一个外键来定位'id'列
)。首先尝试迁移 User
,然后制作 user
表,然后执行 PubSquare
。
编辑:您是否尝试阅读文档? http://sqlalchemy-migrate.readthedocs.org/en/v0 .7.1 / changeset.html#constraint 似乎有帮助。
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(64), unique=True)
# 是不是应该加密下,不能明文存储?应该设置多长的空间? 14.7.18 4:22 by lee
password = db.Column(db.String(100))
nickname = db.Column(db.String(64))
school = db.Column(db.String(20))
sex = db.Column(db.String(5))
status = db.Column(db.String(10))
grade = db.Column(db.String(18))
I have a database remains. Then I add model to models.py:
class PubSquare(db.Model):
id = db.Column(db.Integer, primary_key=True)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
author = db.relationship('User', backref=db.backref('publish'))
subject = db.Column(db.String(100))
timestamp = db.Column(db.DateTime, default=datetime.datetime.now)
Then I run migrate script, it call bug:
NoReferencedTableError: Foreign key associated with column 'pub_square.author_id' could not find table 'user' with which to generate a foreign key to target column 'id'
Befor this time, I can run migrate script successfully for serveral times.But this time, when it refer to foreignkey relationship, it doesn't work.
to prove my models code is right, I re-create the database, it works. So, it's the flask-migrate calls to this bug.
From what I can see the user
table is not being created (could not find table 'user' with which to generate a foreign key to target column 'id'
). Try migrating the User
first and, therefore, making the user
table, and then doing the PubSquare
.
EDIT: Have you tried reading the docs? http://sqlalchemy-migrate.readthedocs.org/en/v0.7.1/changeset.html#constraint seems to help.
这篇关于flask-migrate不起作用当我使用ForeignKey添加模型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!