简体   繁体   English

MySQL变更表给出未知列错误

[英]MySQL alter table gives unknown column error

I'm renaming a bunch of columns in a lot of my tables and changing their data types for a major system update, and I haven't had many issues except for this one. 我正在重命名许多表中的一列列,并更改它们的数据类型以进行主要的系统更新,除此以外,我没有很多问题。

Error Code: 1054. Unknown column 'FactoryID' in 'factories' 错误代码:1054。“工厂”中的未知列“ FactoryID”

show create table `factories`;
CREATE TABLE `factories` (
   `FactoryID` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
   `ParentFactoryID` char(36) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
   `Name` varchar(256) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
   `Notes` varchar(1024) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
   `DateTimeAdded` datetime DEFAULT NULL,
   `CountryID` smallint(5) unsigned NOT NULL,
   `ListID` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
   `Deleted` int(1) DEFAULT '0',
   PRIMARY KEY (`FactoryID`),
   UNIQUE KEY `FactoryID` (`FactoryID`),
   KEY `ParentFactoryID` (`ParentFactoryID`),
   KEY `CountryID` (`CountryID`),
   CONSTRAINT `factories[CountryID]` FOREIGN KEY (`CountryID`) REFERENCES `countries` (`CountryID`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `sterling`.`factories` 
CHANGE COLUMN `CountryID` `CountryID` SMALLINT(5) UNSIGNED NOT NULL AFTER `FactoryID`,
CHANGE COLUMN `ParentFactoryID` `_OriginalFactoryID` CHAR(36) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NULL DEFAULT NULL AFTER `ListID`,
CHANGE COLUMN `DateTimeAdded` `__Added` TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) AFTER `__Active`,
CHANGE COLUMN `Deleted` `__Active` TINYINT(1) NOT NULL DEFAULT 1 ,
ADD COLUMN `__Updated` TIMESTAMP(6) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(6) AFTER `__Added`,
drop primary key,
change column`FactoryID` `@FactoryID`char(36)null after`__Updated`,
add column`FactoryID`binary(8)not null first,
add index`@FactoryID`(`@FactoryID`);

Am I missing an order of operations sort of thing? 我是否缺少某种操作顺序? If all of those changes happen in order, I'm not sure what exactly the problem is, since at no time that FactoryID is referenced does it not exist. 如果所有这些更改都按顺序发生,那么我不确定问题出在FactoryID ,因为在任何时候都FactoryID引用FactoryID

The columnname that after refers to is supposed to be the new name of the column in the final table. after引用的columnname应该是最终表中该列的名称。 It doesn't matter where in the statement you change the name. 更改名称在语句中的位置无关紧要。

And it actually even makes this following statement fail: 实际上,它甚至使以下语句失败:

alter table tablename
add column b int after a,
drop column a 

Error Code: 1054. Unknown column 'a' in 'tablename' 错误代码:1054。“表名”中的未知列“ a”

as in the final table, there will be no column a anymore, so it is invalid even if you drop a only after you already added column b . 就像在最终表中一样,将不再有a列,因此即使您仅在已经添加b列之后才删除a ,它也是无效的。

In your case, you would need to change 就您而言,您需要进行更改

CHANGE COLUMN `CountryID` `CountryID` ... AFTER `FactoryID`,

to

CHANGE COLUMN `CountryID` `CountryID` ... AFTER `@FactoryID`,

in order to anticipate that you will (later) rename the column FactoryID to @FactoryID . 为了期望您(以后)将FactoryID列重命名为@FactoryID

To make it complete: in after , you cannot refer to a column that you will add later. 要使其完整,请执行以下操作:在after ,您不能引用稍后将add的列。 For example, in the end of your statement, you actually add another column FactoryID , but you cannot yet refer to it here (otherwise, the query would not have failed). 例如,在语句末尾,您实际上添加了另一列FactoryID ,但您仍不能在此处引用它(否则,查询不会失败)。 You could add that column first though (and even before you rename the original FactoryId , MySQL allows you to swap columnnames that way). 不过,您可以先添加该列(甚至在重命名原始FactoryId ,MySQL允许您以这种方式交换列名)。 CHANGE COLUMN CountryID CountryID ... AFTER FactoryID would then work, but would refer to the new column (so in total, CountryID would be the 2nd column, which may or may not be what you intended). CHANGE COLUMN CountryID CountryID ... AFTER FactoryID会再工作,但将引用新的列(因此总共CountryID将是第2列,这可能是也可能不是您所希望的)。

I don't know if it is officially documented somewhere, you will probably have to take it as convention, but it has "always" been that way. 我不知道它是否已正式记录在某处,您可能不得不将其作为惯例,但是这种方式“一直”存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM