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

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

解决:

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

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

根据错误提示:

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. 1.删除project内的migrations文件夹
  2.  
  3. 2.python runserver.py db init
  4.  
  5. 3.python runserver.py db migrate -m "write some words"
  6.  
  7. 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. Boxes - SGU 126(找规律)

    题目大意:有两个箱子,一个箱子装了A个球,一个箱子装了B个球,可以从球多的那个箱子拿出来球少的箱子里面球的总数放在少的那个箱子里面,问能否把球全部放在一个箱子里面? 分析:很容易求出来最后放的拿一下一 ...

  2. mac svn命令

    转载:Mac下svn command命令 svn help command  获取子命令说明 svn info $URL 查看工作空间信息 svn list   显示当前目录下svn记录文件列表,不访 ...

  3. kafka集群扩容以及数据迁移

    一 kafka集群扩容比较简单,机器配置一样的前提下只需要把配置文件里的brokerid改一个新的启动起来就可以.比较需要注意的是如果公司内网dns更改的不是很及时的话,需要给原有的旧机器加上新服务器 ...

  4. JSNI GWT中的东东

    二.JavaScript Native InterfaceJavaScript本地接口JSNI.1)声明一个本地方法在JSNI中声明一个本地方法时,使用Java的标准native关键字,就像在JNI( ...

  5. cocos2d-x创建的九宫图变白块

    用UIImageView 创建的九宫图变白,直接用CCScale9Sprite创建的也是变白,找了半天原来是自己为了调整UI方便,开启了CCSprite边缘画线导致的,在ccConfig.h下 宏CC ...

  6. android 05 桢布局:FrameLayout 网格布据 GridLayout

    xml文件: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android: ...

  7. css考核点整理(六)-水平居中定位的几种方式

     定宽    text-align: center 父容器position:relative:子容器 position:absolute;left:50%; margin-left: 宽度/2 .Ce ...

  8. git常见指令

    master : 默认开发分支: origin : 默认远程版本库 初始化操作    $ git config -global user.name <name> #设置提交者名字    $ ...

  9. jQuery Capty 图片标题插件

    jQuery Capty是可以为图片添加漂亮的字幕的插件 文件包含: <link type="text/css" rel="stylesheet" hre ...

  10. 10.3 noip模拟试题

    希望[题目描述]网页浏览器者有后退与前进按钮,一种实现这两个功能的方式是用两个栈,“前进栈”.“后退栈”.这里你需要实现以下几个功能:BACK: 如果“后退栈”为空则忽略此命令. 否则将当前两面压入“ ...