【发布时间】:2017-09-21 07:02:30
【问题描述】:
以下 SQL 脚本适用于 MySQL 5.16.17 及更早版本,但不适用于我的 一个 MySQL 5.7.18 安装(另一个,在 Docker 容器中启动的 MySQL 5.7.18,是也可以)
drop table if exists bar;
drop table if exists foo;
create table foo (foo_id int not null primary key, description varchar(32));
insert into foo values ("1", "foo-one");
insert into foo values ("2", "foo-two");
create table bar (bar_id int not null primary key, foo_id int null, description varchar(32), foreign key (foo_id) references foo(foo_id));
insert into bar values ("1", "1", "bar-one");
insert into bar values ("2", "1", "bar-two");
alter table bar change column foo_id foo_id int not null;
错误信息是:
Error Code: 1832. Cannot change column 'foo_id': used in a foreign key constraint 'bar_ibfk_1'
问题似乎是将具有外键约束的列从 NULL 更改为 NOT NULL。
我知道我可以将最后一条语句包装在“SET foreign_key_checks...”调用中,但我对在这种情况下是否存在影响 MySQL 行为的任何系统变量或配置设置感兴趣,因为我无法解释两个 5.7.18 实例之间的不同行为。
【问题讨论】:
-
我无法重现该问题。您可以告诉我们
SQL_MODE变量的值。见db-fiddle。 -
的sql_mode在5.7.18本地安装:NO_AUTO_CREATE_USER,在5.7.18多克尔容器NO_ENGINE_SUBSTITUTION的sql_mode:ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION(是的,在本地我禁用STRICT_TRANS_TABLES安装实例看看这是否有任何效果,无济于事)
-
使用 db-fiddle,sn-p 以 5.6 失败,并以 5.5 和 5.7 运行。有趣...
标签: mysql foreign-keys alter-table