MySQL无法使用外键创建新表

时间:2013-12-17 16:47:31

标签: mysql sql foreign-keys

我运行时只收到错误150。其他表是InnoDB,类型是主键,数据类型匹配。所以我看不出我做错了什么,(我没有看到明显的语法错误)。有什么想法吗?

CREATE TABLE grouptosite (
  groups_id BIGINT(20),
  usertogroup_groupID BIGINT(20),
usertogroup_userID BIGINT(20),
  usertosite_id INT(10),

  gts_id INT(10) AUTO_INCREMENT NOT NULL,
  PRIMARY KEY (gts_id),
index (gts_id),
index (groups_id),
  index (usertogroup_groupID), 
index (usertogroup_userID),
  index (usertosite_id), 

FOREIGN KEY (groups_id)
    REFERENCES groups(id)
    ON UPDATE CASCADE ON DELETE RESTRICT,
  FOREIGN KEY (usertogroup_groupID, usertogroup_userID)
    REFERENCES usertogroup(groupID, userID)
    ON UPDATE CASCADE ON DELETE RESTRICT,
  FOREIGN KEY (usertosite_id)
    REFERENCES usertosite(id)
    ON UPDATE CASCADE ON DELETE RESTRICT  
) ENGINE=INNODB;

1 个答案:

答案 0 :(得分:1)

您确定您的列类型相同吗?我首先使用ID为BIGINT创建usertosite表(遵循其他表的相同模式),但grouptosite.usertosite_id列为INT:http://sqlfiddle.com/#!2/d821a

如另一条评论中所述,foreign keys的数据类型必须相同。