什么导致integrityerror“cannot add foreign key constraint”?

afdcj2ne  于 2021-06-15  发布在  Mysql
关注(0)|答案(0)|浏览(291)

我的 flask 应用程序在本地运行得非常好。我正在将应用程序部署到python的任何地方。问题是,当我在pythonanywhere bash中运行flask db upgrade时,我得到以下错误(当我在本地进行任何db迁移和升级时,都没有错误):

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'))

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题