虚假外键约束失败

发布于 2024-09-11 13:53:56 字数 826 浏览 5 评论 0 原文

我收到此错误消息:

第 40 行出现错误 1217 (23000):无法 删除或更新父行:a 外键约束失败

...当我尝试删除表时:

DROP TABLE IF EXISTS `area`;

...定义如下:

CREATE TABLE `area` (
  `area_id` char(3) COLLATE utf8_spanish_ci NOT NULL,
  `nombre_area` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
  `descripcion_area` varchar(100) COLLATE utf8_spanish_ci NOT NULL,
  PRIMARY KEY (`area_id`),
  UNIQUE KEY `nombre_area_UNIQUE` (`nombre_area`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;

有趣的是,我已经删除了模式中具有针对 >区域。实际上,除了area表之外,数据库是空的。

如果数据库中没有任何其他对象,它怎么可能有子行?据我所知,InnoDB 不允许在其他模式上使用外键,是吗?

(我什至可以运行 RENAME TABLE area TO Something_else 命令:-?)

I get this error message:

ERROR 1217 (23000) at line 40: Cannot
delete or update a parent row: a
foreign key constraint fails

... when I try to drop a table:

DROP TABLE IF EXISTS `area`;

... defined like this:

CREATE TABLE `area` (
  `area_id` char(3) COLLATE utf8_spanish_ci NOT NULL,
  `nombre_area` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
  `descripcion_area` varchar(100) COLLATE utf8_spanish_ci NOT NULL,
  PRIMARY KEY (`area_id`),
  UNIQUE KEY `nombre_area_UNIQUE` (`nombre_area`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;

The funny thing is that I already dropped all other tables in the schema that have foreign keys against area. Actually, the database is empty except for the area table.

How can it possibly have child rows if there isn't any other object in the database? As far as I know, InnoDB doesn't allow foreign keys on other schemas, does it?

(I can even run a RENAME TABLE area TO something_else command :-?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

夏见 2024-09-18 13:53:56

按需,现在作为答案...

使用 MySQL 查询浏览器或 phpMyAdmin 时,似乎为每个查询打开一个新连接(bugs.mysql.com/bug.php?id=8280),因此有必要在一个查询中编写所有 drop 语句,例如:

SET FOREIGN_KEY_CHECKS=0; 
DROP TABLE my_first_table_to_drop; 
DROP TABLE my_second_table_to_drop; 
SET FOREIGN_KEY_CHECKS=1; 

其中 SET FOREIGN_KEY_CHECKS=1 作为额外的安全措施......

On demand, now as an answer...

When using MySQL Query Browser or phpMyAdmin, it appears that a new connection is opened for each query (bugs.mysql.com/bug.php?id=8280), making it neccessary to write all the drop statements in one query, eg.

SET FOREIGN_KEY_CHECKS=0; 
DROP TABLE my_first_table_to_drop; 
DROP TABLE my_second_table_to_drop; 
SET FOREIGN_KEY_CHECKS=1; 

Where the SET FOREIGN_KEY_CHECKS=1 serves as an extra security measure...

冬天旳寂寞 2024-09-18 13:53:56

两种可能性:

  1. 在另一个模式(mysql 术语中的“数据库”)中存在一个表,该表具有 FK 引用
  2. innodb 内部数据字典与 mysql 不同步。

您可以通过在删除失败后执行“SHOW ENGINE INNODB STATUS”来查看它是哪个表(无论如何,其中之一)。

如果事实证明是后一种情况,如果可以的话,我会转储并恢复整个服务器。

MySQL 5.1 及更高版本将在错误消息中为您提供带有 FK 的表名称。

Two possibilities:

  1. There is a table within another schema ("database" in mysql terminology) which has a FK reference
  2. The innodb internal data dictionary is out of sync with the mysql one.

You can see which table it was (one of them, anyway) by doing a "SHOW ENGINE INNODB STATUS" after the drop fails.

If it turns out to be the latter case, I'd dump and restore the whole server if you can.

MySQL 5.1 and above will give you the name of the table with the FK in the error message.

别挽留 2024-09-18 13:53:56

禁用外键检查

SET FOREIGN_KEY_CHECKS=0

Disable foreign key checking

SET FOREIGN_KEY_CHECKS=0
混吃等死 2024-09-18 13:53:56

来自此博客

您可以暂时禁用外键检查:

SET FOREIGN_KEY_CHECKS=0;

只需确保在完成后恢复它们乱搞:

SET FOREIGN_KEY_CHECKS=1;

from this blog:

You can temporarily disable foreign key checks:

SET FOREIGN_KEY_CHECKS=0;

Just be sure to restore them once you’re done messing around:

SET FOREIGN_KEY_CHECKS=1;
掩耳倾听 2024-09-18 13:53:56

希望它的工作

SETforeign_key_checks = 0;
DROP TABLE 表名;
设置foreign_key_checks = 1;

hopefully its work

SET foreign_key_checks = 0;
DROP TABLE table name;
SET foreign_key_checks = 1;

九公里浅绿 2024-09-18 13:53:56

在 Rails 上,可以使用 rails 控制台 执行以下操作:

connection = ActiveRecord::Base.connection
connection.execute("SET FOREIGN_KEY_CHECKS=0;")

On Rails, one can do the following using the rails console:

connection = ActiveRecord::Base.connection
connection.execute("SET FOREIGN_KEY_CHECKS=0;")
囍笑 2024-09-18 13:53:56

也许您之前使用此表时收到错误。您可以重命名该表并尝试再次删除它。

ALTER TABLE `area` RENAME TO `area2`;
DROP TABLE IF EXISTS `area2`;

Maybe you received an error when working with this table before. You can rename the table and try to remove it again.

ALTER TABLE `area` RENAME TO `area2`;
DROP TABLE IF EXISTS `area2`;
腻橙味 2024-09-18 13:53:56

我找到了一个简单的解决方案,导出数据库,在文本编辑器中编辑您想要编辑的内容,然后导入它。完毕

i found an easy solution, export the database, edit it what you want to edit in a text editor, then import it. Done

爱你不解释 2024-09-18 13:53:56

无法删除或更新父行:外键约束失败 (table1.user_role, CONSTRAINT FK143BF46A8dsfsfds@#5A6BD60 FOREIGN KEY ( user_id) 参考 user (id))

我通过两个简单的步骤做了什么。首先我删除子表中的子行,例如

mysql>从 table2 中删除,其中 role_id = 2 &&用户id=20;

查询正常,1 行受影响(0.10 秒)

,第二步是

从 table1 中删除父级删除,其中 id = 20;

查询正常,1 行受影响(0.12 秒)

通过这个我解决了问题,这意味着删除子项然后删除父项

我希望你明白了。 :)

Cannot delete or update a parent row: a foreign key constraint fails (table1.user_role, CONSTRAINT FK143BF46A8dsfsfds@#5A6BD60 FOREIGN KEY (user_id) REFERENCES user (id))

What i did in two simple steps . first i delete the child row in child table like

mysql> delete from table2 where role_id = 2 && user_id =20;

Query OK, 1 row affected (0.10 sec)

and second step as deleting the parent

delete from table1 where id = 20;

Query OK, 1 row affected (0.12 sec)

By this i solve the Problem which means Delete Child then Delete parent

i Hope You got it. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文