[英]MySql/Cloudbees VARCHAR as ForeignKey
I'm having difficulty getting a Foreign Key on a VARCHAR column. 我很难在VARCHAR列上获取外键。
Ideally I would like to have it as a composite FK (as you can see from my attempt at adding it). 理想情况下,我希望将其作为复合FK(从我添加它的尝试中可以看出)。
Locally, running this script on MySQL 5.6 I get the error # 1215. When running the script on CloudBees I get err 150 在本地,在MySQL 5.6上运行此脚本,我得到错误#1215。在CloudBees上运行脚本时,我得到err 150
I've seen the other questions here on the matter about the data types being exactly the same, I've verified that as you can see from the script. 在这里,我已经看到了关于数据类型完全相同的其他问题,我已经从脚本中验证了这一点。
/* Start of REGION table */
CREATE TABLE REGION (
ID INTEGER NOT NULL AUTO_INCREMENT, /* Unique Identifier */
RORDER INTEGER, /* Order to be displayed by (currently unused) */
LANG VARCHAR(3) DEFAULT 'en' , /* Language of this entry */
NAME VARCHAR(30) NOT NULL, /* Region Name */
DESCR VARCHAR(50) DEFAULT '' NOT NULL, /* Region Desc */
ACTIVE INTEGER DEFAULT '1' NOT NULL, /* Is item active or not */
CDATE TIMESTAMP DEFAULT 0, /* Create Date */
MODDATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, /* Last Update Date */
INSUSER VARCHAR(20) DEFAULT 'SYSTEM' NOT NULL, /* User that inserted this entry */
MODUSER VARCHAR(20), /* User that last modified this entry */
PRIMARY KEY(id)
) engine InnoDB;
COMMIT;
ALTER TABLE REGION ADD CONSTRAINT UNIQUE (LANG, NAME);
/* End of REGION table */
/* Start of LOCATION table */
CREATE TABLE LOCATION (
ID INTEGER NOT NULL AUTO_INCREMENT, /* Unique Identifier */
REGIONNAME VARCHAR(30) NOT NULL, /* FK - REGION.ID */
LORDER INTEGER, /* Order to be displayed by (currently unused) */
NAME VARCHAR(30) DEFAULT '' NOT NULL, /* Location Name (FK Key) */
LANG VARCHAR(3) DEFAULT 'en' , /* Language of this entry */
DESCR VARCHAR(50) DEFAULT '' NOT NULL, /* Location Desc */
ACTIVE INTEGER DEFAULT '1' NOT NULL, /* Is item active or not */
CDATE TIMESTAMP DEFAULT 0, /* Create Date */
MODDATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, /* Last Update Date */
INSUSER VARCHAR(20) DEFAULT 'SYSTEM' NOT NULL, /* User that inserted this entry */
MODUSER VARCHAR(20), /* User that last modified this entry */
PRIMARY KEY(id)
) engine InnoDB;
ALTER TABLE LOCATION ADD CONSTRAINT FK_LOCATION_REGION FOREIGN KEY (REGIONNAME, LANG) REFERENCES REGION(NAME, LANG) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE LOCATION ADD CONSTRAINT UNIQUE (LANG, NAME);
COMMIT;
/* End of LOCATION table */
It required an index on the columns in the REGION table. 它要求在REGION表中的列上有一个索引。
Solution: 解:
After REGION is created but before LOCATION is created, add: 在创建REGION之后但在创建LOCATION之前,添加:
CREATE INDEX REG_NAME_LANG ON REGION (NAME, LANG);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.