Flask_SqlAlchemy 1215, 'Cannot add f oreign key constraint'報錯html
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)']
解決:python
使用以下命令查看報錯信息mysql
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 ============================ | +--------+------+--------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- --------------------------------------------------------------------------------
根據錯誤提示:sql
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.flask
刪除數據庫內全部表 app
再執行oop
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