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
  • 相关阅读:
    ARM板卡ftp客户端应用
    vsftp移植(待续)
    /dev/null脚本中作用
    amazeui.css
    将td中文字过长的部分变成省略号显示的小技巧
    div非弹出框半透明遮罩实现全屏幕遮盖css实现
    重新定位svn地址的方法(windows和linux),svn switch(sw)的帮助信息
    linux 下启动SVN服务
    用SVN checkout源码时,设置账号
    svn提示out of date
  • 原文地址:https://www.cnblogs.com/yaohan/p/6109652.html
Copyright © 2011-2022 走看看