gpt4 book ai didi

mysql - InnoDB 外键定义无法解析列名

转载 作者:行者123 更新时间:2023-11-29 06:55:00 25 4
gpt4 key购买 nike

我在两个表之间创建外键约束时遇到问题。

这是第一个表:

CREATE TABLE `agews_rifiuti_cer` (
`id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,
`agews_id` int(10) unsigned DEFAULT '0',
`livello` tinyint(4) DEFAULT '1',
`codice` varchar(10) DEFAULT NULL,
`descrizione` varchar(255) DEFAULT NULL,
`note` text,
`flag_pericoloso` tinyint(1) DEFAULT '0',
`id_cliente` int(10) unsigned DEFAULT '1',
`flag_modificato` char(1) DEFAULT 'N',
PRIMARY KEY (`id_cer`),
KEY `fk_id_cliente_agews_sgs_codici_cer` (`id_cliente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

这是第二个:

CREATE TABLE `lin_98_47_rifiuti` (
`id_rifiuto` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_azienda` int(10) unsigned NOT NULL DEFAULT '1',
`id_sede` int(10) unsigned NOT NULL DEFAULT '1',
`revisione_documento` int(10) unsigned NOT NULL DEFAULT '0',
`rifiuto` varchar(255) DEFAULT NULL,
`codice_cer` varchar(10) DEFAULT NULL,
`nome_interno` varchar(255) DEFAULT NULL,
`descrizione` text,
`materie_prime` text,
`contenitore` text,
`deposito` text,
`data_ultima_analisi` date DEFAULT NULL,
`stato_fisico` enum('Solido pulverulento','Solido non pulverulento','Fangoso palabile','Liquido') DEFAULT 'Solido non pulverulento',
`quantita` float(9,1) DEFAULT '0.0',
`unita_misura` enum('Kg','l','mc') DEFAULT 'Kg',
`id_pericolo` int(10) unsigned DEFAULT NULL,
`destino` enum('Recupero','Smaltimento') DEFAULT NULL,
`id_recupero` int(10) unsigned DEFAULT NULL,
`id_smaltimento` int(10) unsigned DEFAULT NULL,
`id_cer` int(10) unsigned DEFAULT NULL,
`immagine` varchar(255) DEFAULT NULL,
`image_type` varchar(20) DEFAULT NULL,
`image_content` mediumblob,
`image_size_x` smallint(5) unsigned DEFAULT '0',
`image_size_y` smallint(5) unsigned DEFAULT '0',
`flag_storico` tinyint(1) DEFAULT '0',
`id_responsabile` int(10) unsigned DEFAULT '0',
`nome_responsabile` varchar(255) DEFAULT '0',
`id_ultima_modifica` int(10) unsigned DEFAULT '0',
`create_log` tinyint(1) DEFAULT '1',
PRIMARY KEY (`id_rifiuto`,`id_azienda`,`id_sede`,`revisione_documento`),
KEY `fk_main_lin_98_47_rifiuti` (`id_azienda`,`id_sede`,`revisione_documento`),
KEY `fk_id_responsabile_lin_98_47_rifiuti` (`id_responsabile`,`id_azienda`,`id_sede`,`revisione_documento`,`nome_responsabile`),
KEY `fk_id_pericolo_lin_98_47_rifiuti` (`id_pericolo`),
KEY `fk_id_recupero_lin_98_47_rifiuti` (`id_recupero`),
KEY `fk_id_smaltimento_lin_98_47_rifiuti` (`id_smaltimento`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

问题发生在我尝试这样做时:

ALTER TABLE lin_98_47_rifiuti ADD CONSTRAINT fk_id_cer_lin_98_47_rifiuti FOREIGN KEY (id_cer) REFERENCES agews_rifiuti_cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE;

我得到这个错误:

#1005 - Can't create table 'db_626suite.#sql-71c_13d5' (errno: 150)

虽然命令 SHOW INNODB STATUS 说:

Error in foreign key constraint of table db_626suite/#sql-71c_13d5:
FOREIGN KEY (id_cer) REFERENCES agews_rifiuti_cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE:
Cannot resolve column name close to:
) ON UPDATE CASCADE ON DELETE CASCADE

但语法和字段定义对我来说似乎是正确的。我做错了什么?

编辑:

Marc B 认为这可能是因为 id_ceragews_rifiuti_cer 中被定义为 NOT NULL >,但我不认为是这种情况,实际上请考虑这个其他表:

CREATE TABLE `agews_rifiuti_pericolo` (
`id_pericolo` int(10) unsigned NOT NULL AUTO_INCREMENT,
`agews_id` int(10) unsigned DEFAULT '0',
`codice` varchar(255) DEFAULT NULL,
`pericolo` varchar(255) DEFAULT NULL,
`note` text,
`id_cliente` int(10) unsigned DEFAULT '1',
`flag_modificato` char(1) DEFAULT 'N',
PRIMARY KEY (`id_pericolo`),
KEY `codice_rifiuti_recupero` (`codice`),
KEY `fk_id_cliente_rifiuti_pericolo` (`id_cliente`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

这里 id_pericolo 被定义为 NOT NULL 就像上面的 id_cer 一样,但是这完美地工作:

ALTER TABLE lin_98_47_rifiuti ADD CONSTRAINT fk_id_pericolo_lin_98_47_rifiuti FOREIGN KEY (id_pericolo) REFERENCES agews_rifiuti_pericolo(id_pericolo) ON UPDATE CASCADE ON DELETE SET NULL;

即使在 lin_98_47_rifiuti 中,字段 id_pericolo 被定义为 DEFAULT NULL

编辑 2:

我刚刚尝试了这个最小的设置:

CREATE TABLE `cer` (
`id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,
`codice` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id_cer`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `rifiuti` (
`id_rifiuto` int(10) unsigned NOT NULL AUTO_INCREMENT,
`id_cer` int(10) unsigned NULL DEFAULT NULL,
`rifiuto` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id_rifiuto`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE rifiuti ADD FOREIGN KEY (id_cer) REFERENCES cer(id_cer) ON UPDATE CASCADE ON DELETE CASCADE;

一切正常,但我不明白我的原始查询中可能存在什么问题。

另外我认为这证明了将字段定义为 NOT NULLDEFAULT NULL 不是问题。

最佳答案

您在 lin_98_47 表中将 id_cer 定义为 default null,但在另一个表中匹配的 FK 字段定义为 not null.

字段定义必须完全匹配才能建立外键关系,包括可空性:

`id_cer` int(10) unsigned NOT NULL AUTO_INCREMENT,

`id_cer` int(10) unsigned DEFAULT NULL,

关于mysql - InnoDB 外键定义无法解析列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13293941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com