简体   繁体   English

无法在删除时添加“set default”的外键

[英]Cannot add foreign key with “set default” as on delete

I'm using MariaDB and now wanted to add a foreign key to my database table. 我正在使用MariaDB,现在想在我的数据库表中添加一个外键。 I'm having problems when trying to use set default as my on delete constraint;=. 我在尝试使用set default作为我的删除约束时遇到问题; =。 (The default of the user_id in the pages table is 1) pages表中user_id的默认值为1)

`alter table `pages` add constraint pages_user_id_foreign foreign key (`user_id`) references `users` (`id`) on delete set default`

However, when I'm doing this, I'm getting the error 但是,当我这样做时,我收到了错误

SQLSTATE[HY000]: General error: 1005 Can't create table 'project.#sql-add_38' (errno: 150)

When I look at the last foreign key error, I'm getting the following output 当我查看最后一个外键错误时,我得到以下输出

160322 12:40:56 Error in foreign key constraint of table project . 160322 12:40:56表project外键约束出错 pages : Alter table project . pages :更改表project pages with foreign key constraint failed. 具有外键约束的pages失败。 Parse error in ' foreign key ( user_id ) references users ( id ) on delete set default' near ' on delete set default'. '外键( user_id )中的解析错误引用删除时的usersid )默认'删除集'默认'附近'。

When I do it without on delete set default it works without any problems 如果我没有on delete set default它没有任何问题

`alter table `pages` add constraint pages_user_id_foreign foreign key (`user_id`) references `users` (`id`)`

Same goes, if I use set null for my on delete condition 同样,如果我在删除条件中使用set null

alter table `pages` add constraint pages_provider_id_foreign foreign key (`provider_id`) references `providers` (`id`) on delete cascade

Somehow, only the set default doesn't work, but the error message doesn't give me enough information 不知何故,只有set default不起作用,但错误消息不能提供足够的信息

Ok, got it. 好的,我知道了。 Seems like InnoDB doesn't allow for ON DELETE SET DEFAULT 似乎InnoDB不允许ON DELETE SET DEFAULT

SET DEFAULT: This action is recognized by the MySQL parser, but both InnoDB and NDB reject table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses. SET DEFAULT:MySQL解析器识别此操作,但InnoDB和NDB都拒绝包含ON DELETE SET DEFAULT或ON UPDATE SET DEFAULT子句的表定义。

https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM