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

Flask_SqlAlchemy 1215, 'Cannot add f oreign key constraint'的更多相关文章

  1. MySQL添加foreign key时出现1215 Cannot add the foreign key constraint

    引言: MySQL中经常会需要创建父子表之间的约束,这个约束是需要建立在主外键基础之上的,这里解决了一个在创建主外键约束过程中碰到的一个问题. mysql中添加外键约束遇到一下情况: cannot a ...

  2. MySQL添加外键时报错 ERROR 1215 (HY000): Cannot add foreign key constraint

    1.数据类型      2.数据表的引擎 数据表 mysql> show tables; +------------------+ | Tables_in_market | +--------- ...

  3. mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决

    ERROR 1215 (HY000): Cannot add foreign key constraint 最近在建表时遇到了这个错误,然后找了下找到了解决办法,记录下: 本来是要建两张表: 1 2 ...

  4. Laravel 5.5 迁移报错:General error: 1215 Cannot add foreign key constraint

    问题 之前一直用的 Laravel 5.4,数据库也是直接写 sql 的,感觉可定制性更强,顺便锻炼下 sql.这次改用了 Laravel 5.5,索性用迁移建库试试,结果报错如下: SQLSTATE ...

  5. 异常处理:1215 - Cannot add foreign key constraint

    最近在做新生入学系统,学生表中包括新生的班级,专业等信息,班级,专业就需要和班级表,专业表进行关联,但是在添加外键的过程中却出现了“Cannot add foreign key constraint” ...

  6. Error 'Cannot add or update a child row: a foreign key constraint fails故障解决

    一大早的,某从库突然报出故障:SQL线程中断! 查看从库状态: mysql> show slave status\G Slave_IO_State: Waiting for master to ...

  7. insert时报Cannot add or update a child row: a foreign key constraint fails (`yanchangzichan`.`productstatusrecord`, CONSTRAINT `p_cu` FOREIGN KEY (`cid`) REFERENCES `customer` (`cid`))错误

    mybatis在insert时报Cannot add or update a child row: a foreign key constraint fails (`yanchangzichan`.` ...

  8. IntegrityError at /admin/users/userprofile/add/ (1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_

    报错现象 在执行 django 后台管理的时候添加数据导致 1452 错误 报错代码 IntegrityError at /admin/users/userprofile/add/ (1452, 'C ...

  9. Cannot add foreign key constraint @ManyToMany @OneToMany

    最近在使用shiro做权限管理模块时,使用的时user(用户)-role(角色)-resource(资源)模式,其中user-role 是多对多,role-resource 也是多对多.但是在使用sp ...

随机推荐

  1. Apache 一台主机绑定多个域名及虚拟主机

    今天研究了下Apache下如何使用一台主机绑定多个域名且使用80端口.说白了就是在一台主机上运行多个网站,并且网站域名都是使用的80端口. 具体方法如下: 1.进入Apache conf目录,找到ht ...

  2. luajit 安装cjson

    最近需要升级原有服务器的nginx加载逻辑,新的lua脚本需要解析一个远程返回的json格式的结果,原有的luajit并没有带cjson库,需要自己手动安装一下. 基本参考博客luajit安装cjso ...

  3. 【Android - 框架】之Retrofit+RxJava的使用

    前几天分别对Retrofit和RxJava进行了总结,这个帖子打算把Retrofit结合RxJava使用的方法总结以下.有还不了解Retrofit或RxJava的朋友可以参考下面的帖子学习~ [And ...

  4. [Oracle] Data Pump 详细使用教程(4)- network_link

    [Oracle] Data Pump 详细使用教程(1)- 总览 [Oracle] Data Pump 详细使用教程(2)- 总览 [Oracle] Data Pump 详细使用教程(3)- 总览 [ ...

  5. CSU1312:榜单(模拟)

    Description ZZY很喜欢流行音乐,每周都要跟踪世界各地各种榜单,例如Oricon和Billboard,现在给出每周各个单曲的销量请给出每周的TOP5以及TOP5中各个单曲的浮动情况. 量的 ...

  6. Android 手动显示和隐藏软键盘

    1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Contex ...

  7. ReentrantLock与synchronized的差别

    总的来说,lock更加灵活. 主要同样点:Lock能完毕synchronized所实现的全部功能 不同: 1.ReentrantLock功能性方面更全面,比方时间锁等候,可中断锁等候,锁投票等,因此更 ...

  8. 自定义的插件如何加载到Qt Designer中(详细)

    要想在Qt Designer中使用自定义控件,必须要使Qt Designer能够知道我们的自定义控件的存在.有两种方法可以把新自定义控件的信息通知给Qt Designer:“升级(promotion) ...

  9. 如何判断JDK是32位还是64位

    第一种方法 在CMD窗口中使用java -version 命令进行查看 如果是64位的则会显示 Java HotSpot<TM>64-Bit 字样,32位的则没有类似信息. 注:这是Sun ...

  10. Xcode常见报错及解决办法

    报错一: 在iOS7的真机运行时,弹出错误:App installation failed. There was an internal API error. 如图 解决办法: 在Xcode -> ...