Flask_SqlAlchemy 1215, 'Cannot add f oreign key constraint'

Flask_SqlAlchemy 1215, 'Cannot add f oreign key constraint'报错

 

sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1215, 'Cannot add f
oreign key constraint') [SQL: 'ALTER TABLE users ADD FOREIGN KEY(user_group) REF
ERENCES groups (id)']

 

解决:

使用如下命令查看报错信息

 SHOW ENGINE INNODB STATUS

 

| InnoDB |      |
=====================================
2016-11-28 14:15:30 18c4 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 31 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 33 srv_active, 0 srv_shutdown, 19178 srv_idle
srv_master_thread log flush and writes: 19211
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 45
OS WAIT ARRAY INFO: signal count 47
Mutex spin waits 19, rounds 237, OS waits 7
RW-shared spins 31, rounds 930, OS waits 31
RW-excl spins 5, rounds 214, OS waits 7
Spin rounds per wait: 12.47 mutex, 30.00 RW-shared, 42.80 RW-excl
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2016-11-28 13:59:59 18c4 Error in foreign key constraint of table flask/#sql-69c
_30:
FOREIGN KEY(user_group) REFERENCES groups (id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
----------------------------------------
END OF INNODB MONITOR OUTPUT
============================
 |
+--------+------+---------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

 

 

根据错误提示:

cannot be referenced by such columns in new tables.

See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html for correct foreign key definition.

 

删除数据库内所有表  

再执行

1.删除project内的migrations文件夹

2.python runserver.py db init

3.python runserver.py db migrate -m "write some words"

4.python runserver.py db upgrade

 

转载于:https://www.cnblogs.com/yaohan/p/6109652.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值