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. OpenSSL使用指南

    OpenSSL使用指南 1     介绍 OpenSSL是使用非常广泛的SSL的开源实现.由于其中实现了为SSL所用的各种加密算法,因此OpenSSL也是被广泛使用的加密函数库. 1.1   SSL ...

  2. android 34 ListView进阶

    public View getView(int position, View convertView, ViewGroup parent) {////convertView是一个缓存,每次返回一个la ...

  3. SqlLite 简明教程

    SQL DML 和 DDL        可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL).         注:"--"双减号为行注释     ...

  4. Linux下使用Eclipse开发C/C++程序

          相信好多人和我一样困惑,在网上查各种安装配置方法,可是试了所有的方法也还是没有成功,其实,这个并不能怪网上的方法不对,可能只是你没有点击一个键的原因,下面,我就来讲下怎样使用Eclipse ...

  5. hdu 4622 Reincarnation(后缀数组)

    hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...

  6. windows 配置host

    windows 下如何配置host文件, 只需修改: C:\Windows\System32\drivers\etc\hosts文件即可 默认文件内容如下: # Copyright (c) 1993- ...

  7. Java基础知识强化之集合框架笔记41:Set集合之HashSet存储自定义对象并遍历练习

    1. HashSet集合存储自定义对象并遍历.如果对象的成员变量值相同即为同一个对象 注意了: 你使用的是HashSet集合,这个集合的底层是哈希表结构. 而哈希表结构底层依赖:hashCode()和 ...

  8. C#解leetcode 64. Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. 如何使用node中的buffer

    介绍:Buffer类是一个全局类,是一个比较罕见不需要require( ‘buffer’ )就可以使用的类,Buffer类似与数组也有length, 它里面的元素为16进制的两位数,即 0-255的数 ...

  10. 自己写的demo。List<HashMap<String,Object>>=new ArrayList<HashMap<String,Object>>

    package com.pb.collection; import java.util.ArrayList; import java.util.HashMap; import java.util.It ...