首頁  >  問答  >  主體

無法刪除索引“*”:外鍵約束中需要 -> 但是是哪一個?

上下文

我在從一個表中刪除一行時遇到問題。

我使用這些查詢首先刪除它的外鍵,然後刪除鍵指向的列。

ALTER TABLE resources drop foreign key fk_res_to_addr;
    ALTER TABLE resources drop column address_id;

刪除約束效果很好。刪除列失敗,並顯示 Cannot drop index 'fk_res_to_addr': required in aforeign 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);

P粉155710425P粉155710425207 天前345

全部回覆(2)我來回復

  • P粉145543872

    P粉1455438722024-03-26 20:06:21

    如果你願意嘗試

    SHOW INDEXES FROM database_name.table_name;

    它可能會告訴您fk_res_to_addr'是否真的被刪除。

    回覆
    0
  • P粉068510991

    P粉0685109912024-03-26 16:29:38

    我以前沒有遇到過這個,但它太糟糕了,我懷疑可能有 mysql 錯誤。它在 mariadb 中「正常」工作。

    您應該看到錯誤Can't DROP 'fk_res_to_addr';檢查列/鍵是否存在以及您報告的錯誤- 請參閱https://dbfiddle .uk/?rdbms=mysql_5.7&fiddle=c5b0bbc9d6c12f74e00ba8d059a1563

    ##在建立時,mysql 使用您指派給 fk 的名稱建立索引,並將自己的名稱指派給 fk。結果是上面提到的錯誤加上

    https://dbfiddle.uk/?rdbms=mysql_5。 7&fiddle=c5b0bbc9d6c12f74e00ba8d059a15638

    我建議您嘗試刪除鍵,然後是外鍵,然後是列。

    回覆
    0
  • 取消回覆