MySQL errorno 121

為{幸葍}努か 提交于 2019-11-27 13:52:57

Check that all your constraints are really spelled out correctly, also check that there's not any other tables that uses the constraint names FK_userId or FK_blogId

Error 121 is a foreign key constraint issue. The first thing to check is that your foreign key definitions are ok (all the tables and field names are correct, etc.).

You can try disabling foreign key checks before creating the table as well, like this:

SET FOREIGN_KEY_CHECKS = 0;

That has the downside of throwing errors later when you re-enable your key checks (set it to 1), however, if this is the case, then it means you have some invalid records somewhere that are interfering with the creation of the foreign key.

However, this issue can also occur if you've been manually moving database files around, such as physically renaming the data/your_database_name directory. InnoDB can't correlate physical changes like that to the tablespace, so it mucks with the internals.

If this is what you did, the solution that works best is to move your old database back to where it was, take a dump or export of it, and do a DROP DATABASE on it before re-importing.

please check that your foreign key which you are creating is same in all aspects such as datatype with the referred table column . Each foreign key name should be unique for those tables in which it is created , it should not be used in anyother tables. for the above problem the Foreign key name "FK_userId" should not be used in any other table .

In I've this problem in mysql 5.5 but works fine in mysql 5.6. The problem was because the constraint name looks to be unique but if this is a long name and is truncated the become non unique, for example :

long_constraint_name_1, long_constraint_name_2 may become long_constraint_name_

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!