語境
我在從我們的一個表中洗掉一行時遇到問題。
我使用這些查詢首先洗掉它的外鍵,然后洗掉鍵指向的列。
ALTER TABLE resources drop foreign key fk_res_to_addr;
ALTER TABLE resources drop column address_id;
洗掉約束效果很好。洗掉列失敗,出現Cannot drop index 'fk_res_to_addr': needed in a foreign key constraint.
到目前為止我嘗試了什么
我首先嘗試(并且仍在嘗試)找出仍然依賴該索引的內容。我使用了這個查詢(在這個答案中找到):
SELECT
TABLE_NAME,
COLUMN_NAME,
CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
TABLE_SCHEMA = 'some_db' AND
REFERENCED_TABLE_NAME = 'resources';
但是那里什么都沒有。
然后我嘗試通過以下方式禁用檢查:
SET FOREIGN_KEY_CHECKS=0;
然后當然是在之后重新啟用它們。這也沒有效果。
我還能做些什么來弄清楚什么依賴于這個索引?還有什么我想念的嗎?
** 編輯 - 請求的表定義** 這是現在的表定義。如您所見,address_id 現在有外鍵,但索引仍然存在。
create table resources
(
id bigint auto_increment primary key,
created bigint not null,
lastModified bigint not null,
uuid varchar(255) not null,
description longtext null,
internalName varchar(80) null,
publicName varchar(80) not null,
origin varchar(80) null,
archived bigint unsigned null,
contact_id bigint null,
colorClass varchar(80) null,
address_id bigint null,
url mediumtext null,
constraint uuid
unique (uuid),
constraint FK_contact_id
foreign key (contact_id) references users (id)
)
charset = utf8;
create index fk_res_to_addr on resources (address_id);
create index idx_resources_archived on resources (archived);
create index idx_resources_created on resources (created);
uj5u.com熱心網友回復:
我以前沒有遇到過這種情況,但它太糟糕了,我懷疑可能存在 mysql 錯誤。它在 mariadb 中“正常”作業。
您應該看到一個錯誤 Can't DROP 'fk_res_to_addr'; 檢查列/鍵是否存在以及您報告的錯誤 - 請參閱https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=c5b0bbc9d6c12f74e00ba8d059a15638
在創建時,mysql 使用您分配給 fk 的名稱創建一個索引,并將它自己的名稱分配給 fk。結果是上面提到的錯誤加上https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=c5b0bbc9d6c12f74e00ba8d059a15638。
我建議您嘗試洗掉鍵,然后是外鍵,然后是列。
uj5u.com熱心網友回復:
如果你會嘗試
SHOW INDEXES FROM database_name.table_name;
它可能會告訴你fk_res_to_addr'是否真的被洗掉了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/496284.html
