2014-02-16 61 views
1

我很難在VARCHAR列上獲取外鍵。MySql/Cloudbees VARCHAR作爲外鍵

理想情況下,我想把它作爲一個複合FK(正如你可以看到我從添加它的嘗試)。

本地,在MySQL 5.6運行此腳本我得到錯誤#1215 當CloudBees的我得到犯錯150

我見過有關數據類型是這裏的其他問題就此事運行腳本完全一樣,我已經證實,你可以從腳本中看到。

/* 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 */ 
+0

糟糕!我忘了更新/ *註釋* /什麼是外鍵字段.../FK - REGION.ID /現在應讀取/等待來自stackoverflow用戶/ –

回答

0

它需要在REGION表中的列的索引。

解決方案:

區之後創建,但在創建位置之前,添加:

CREATE INDEX REG_NAME_LANG ON REGION (NAME, LANG);