0

错误地我添加了两个外键引用同一个表和列。SHOW CREATE TABLE table_a看起来像:

table_a | CREATE TABLE `table_a` (
`id` char(36) NOT NULL,
`fk` int(11) default NULL,
`created_at` datetime default NULL,
PRIMARY KEY  (`id`),
KEY `fk` (`fk`),
CONSTRAINT `table_a_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `table_b` (`id`),
CONSTRAINT `table_a_fkey` FOREIGN KEY (`fk`) REFERENCES `table_b` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

此时我唯一的解决方案是遍历列上的所有键/约束并删除它们,然后再添加一次级联键。

有没有办法使用SQLAlchemy Migrate仅删除非级联约束?

4

1 回答 1

0

你试过了吗?

cons = ForeignKeyConstraint(columns=[table.c.fk],
                            refolumns=[table_b.c.id],
                            name="table_a_ibfk_1")
cons.drop(engine=migrate_engine)
于 2012-12-10T13:30:42.177 回答