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
  • 相关阅读:
    最小生成树(prim算法)C语言实现
    图的十字链表存储(C语言)
    打算做一款给方便学生生活的APP,虽然已经有口袋小安了,但是并没有我想要的功能。。。
    腾讯云Ubuntu安装JDK和Tomcat
    新浪微博配图批量下载
    Algorithms in C第一章笔记
    Java笔记
    稍微记录一下effective c++的一些东西
    总结,并加油
    苹果Xcode帮助文档阅读指南
  • 原文地址:https://www.cnblogs.com/yaohan/p/6109652.html
Copyright © 2011-2022 走看看