这是我的SQL:
CREATE TABLE mytable (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(10) NOT NULL,
`token` varchar(100),
`created_at` datetime,
PRIMARY KEY (`id`),
CONSTRAINT `FK_PASSWORD_RECOVERY_CUSTOMER` FOREIGN KEY (`customer_id`) REFERENCES `customer_entity` (`entity_id`) ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
它总是给我:无法创建表'mytable'(错误号:150)SQL.sql
答案 0 :(得分:0)
正如我对您的问题的评论中所述,您获得的错误与约束定义有关。然后,您应该检查proper syntax for the constraint。
我认为问题在于您没有为customer_id
列编制索引。尝试索引它:
CREATE TABLE mytable (
`id` int(11) NOT NULL AUTO_INCREMENT,
`customer_id` int(10) NOT NULL,
`token` varchar(100),
`created_at` datetime,
PRIMARY KEY (`id`),
INDEX cust_id(customer_id), -- ADD THIS
FOREIGN KEY (`customer_id`)
REFERENCES `customer_entity` (`entity_id`)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
(作为预防措施和哑巴检查,确认customer_entity
也是InnoDB表)