https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint
class CaseUsers(db.Model):
__tablename__ = 'case_users'
id = db.Column(db.BIGINT, primary_key=True, autoincrement=True)
user_id = db.Column(db.String(20), db.ForeignKey('user.id'), nullable=False)
case_id = db.Column(db.BigInteger, db.ForeignKey('case.id'), nullable=False)
role = db.Column(db.SMALLINT, nullable=False)
sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1215, u'Cannot add foreign key constraint') [SQL: u'\nCREATE TABLE case_users (\n\tid BIGINT NOT NULL AUTO_INCREMENT, \n\tuser_id VARCHAR(20) NOT NULL, \n\tcase_id BIGINT NOT NULL, \n\t`role` SMALLINT NOT NULL, \n\tPRIMARY KEY (id), \n\tFOREIGN KEY(user_id) REFERENCES user (id), \n\tFOREIGN KEY(case_id) REFERENCES `case` (id)\n)\n\n']
只带哦
CREATE TABLE `case_users` (
`case_id` bigint(20) DEFAULT NULL,
`user_id` varchar(20) DEFAULT NULL,
`role` smallint(6) DEFAULT NULL,
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
KEY `case_id` (`case_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `case_users_ibfk_1` FOREIGN KEY (`case_id`) REFERENCES `case` (`id`),
CONSTRAINT `case_users_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=252 DEFAULT CHARSET=utf8;
解决:
MySQL数据库默认的编码必须是utf-8, 默认引擎是InnoDB