我做:
app/console d:s:u --dump-sql
它显示了我
ALTER TABLE coupon ADD CONSTRAINT FK_64BF3F02A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE;
没关系,所以我尝试执行:
app/console d:s:u --dump-sql --force
它向我展示:
ALTER TABLE coupon ADD CONSTRAINT FK_64BF3F02A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE;
Updating database schema...
Database schema updated successfully! "1" queries were executed
我尝试检查一切是否还可以并执行:
app/console d:s:u --dump-sql
,它再次向我展示:
ALTER TABLE coupon ADD CONSTRAINT FK_64BF3F02A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE;
有什么问题?我没有错误,但也没有结果。
从MySQL进行所有操作后创建表:
CREATE TABLE `coupon` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code` varchar(255) NOT NULL DEFAULT '',
`months` tinyint(1) NOT NULL,
`billed` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Require CC details or not',
`email` varchar(255) DEFAULT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`used_at` datetime DEFAULT NULL,
`expired_at` datetime DEFAULT NULL,
`deleted` tinyint(1) NOT NULL DEFAULT '0',
`multiuser` tinyint(1) NOT NULL DEFAULT '0',
`days` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `IDX_64BF3F02A76ED395` (`user_id`)
) ENGINE=MyISAM AUTO_INCREMENT=7154 DEFAULT CHARSET=utf8
问题是您在表中使用的引擎。Myisam无法存储外国钥匙,而MySQL只是默默地忽略了任何试图添加它们的SQL。我建议将InnoDB用作所有列的发动机(有关差异,请参见Myisam对InnoDB)
ALTER TABLE `coupon` ENGINE = InnoDB ;
然后您可以再次执行外键SQL
ALTER TABLE `coupon`
ADD CONSTRAINT `FK_64BF3F02A76ED395`
FOREIGN KEY (`user_id`)
REFERENCES `user` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;