是什么引起IntegrityError'无法添加外键约束'?

时间:2019-01-07 11:26:45

标签: mysql sqlalchemy flask-sqlalchemy pythonanywhere flask-migrate

我的烧瓶应用程序在本地绝对好用。我正在将应用程序部署到Python Anywhere。问题是,当我在pythonanywhere bash中运行 flask数据库升级时,出现以下错误(当我执行任何数据库迁移并在本地升级时,没有错误):

sqlalchemy.exc.IntegrityError: (_mysql_exceptions.IntegrityError) 
(1215, 'Cannot add foreign key constraint') [SQL: '\nCREATE TABLE 
comments (
\n\tid INTEGER NOT NULL AUTO_INCREMENT, \n\tbody TEXT, \n\ttimestamp 
DATETIME, \n\tdisable BOOL, \n\tauthor_id INTEGER, \n\tpost_id 
INTEGER, \
n\tPRIMARY KEY (id), \n\tFOREIGN KEY(author_id) REFERENCES users 
(id), \n\tFOREIGN KEY(post_id) REFERENCES posts (id), \n\tCHECK 
(disable IN (
0, 1))\n)\n\n']

我已经检查了其他类似的问题,并且知道此错误的可能根源,但是我没有提到任何问题。这是评论模型:

class Comment(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.Text)
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    disable = db.Column(db.Boolean)
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))

0 个答案:

没有答案