我在 InnoDB 中有一个表,它使用基于 VARCHAR 的外键:
CREATE TABLE `portal_equity` (
`isin` varchar(12) NOT NULL,
....,
PRIMARY KEY (`isin`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8
另一个表对该表有外键约束:
CONSTRAINT `equity_id_refs_isin_2581bb1d` FOREIGN KEY (`equity_id`) REFERENCES `portal_equity` (`isin`)
我需要更改主键 VARCHAR 列的长度,所以我做到了。现在,每次我想引用“portal_equity”表中 isin 超过原始长度 12 的行时,我都会不断收到以下完整性错误。
Foreign key constraint fails for table
investtor
.portal_equitypastvalue
: CONSTRAINTequity_id_refs_isin_7eed44e7
FOREIGN KEY (equity_id
) REFERENCESportal_equity
(isin
) Trying to add in child table, in indexportal_equitypastvalue_equity_id_6e7526e1
tuple: DATA TUPLE: 3 fields; 0: len 12; hex 304c55303332333133343030; asc 0LU032313400;; 1: len 3; hex 8fb578; asc x;; 2: len 4; hex 8001398a; asc 9 ;;But in parent table
investtor
.portal_equity
, in indexPRIMARY
, the closest match we can find is record: PHYSICAL RECORD: n_fields 9; compact format; info bits 0 0: len 13; hex 304c5530333233313334303036; asc 0LU0323134006;; 1: len 6; hex 000000005139; asc Q9;; 2: len 7; hex 800000002d0110; asc - ;; 3: len 4; hex 61736466; asc asdf;; 4: len 4; hex 80000050; asc P;; 5: len 3; hex 736466; asc sdf;; 6: len 4; hex 80000001; asc ;; 7: len 2; hex 4144; asc AD;; 8: len 0; hex ; asc ;;
我需要重新生成所有外键约束吗?这似乎有点过分了。此行为/错误是否记录在某处?
最佳答案
Do I need to regenerate all foreignkey constraints?
是的。
That seems rather excessive.
如果您更改主键字段的数据类型,在我看来,外键字段(及其各自的索引和约束)的数据类型也需要更改。
尽管 documentation声明“字符串类型的长度不必相同”,逻辑上讲外键字段的长度至少应与您要在其中存储的任何数据一样长。
祝你好运!
关于mysql - InnoDB:更改 VARCHAR 主键的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4271147/