zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    【转载】[教程]OpenSEES超简单易懂的入门第一课
    【转载】面向对象的非线性有限元方法
    与李文雄老师讨论有限元
    【转载】 Moving Beyond OpenGL 1.1 for Windows
    【转载】国外免费期刊全文数据库
    与李文雄老师讨论学术研究
    【转载】VS 2010和.NET 4.0之WPF 4改进全解析
    【转载】MFC中SDI、MDI框架各部分指针获取(网上找的,好东西大家一起分享,多谢原创作者!)
    【转载】一位院士——搞科研的几个条件
    Visual Studio 2010 step by step学习摘要
  • 原文地址:https://www.cnblogs.com/yaohan/p/6109652.html
Copyright © 2011-2022 走看看