自引用外键 - mysql未设置为null

时间:2022-06-21 21:02:31

Indexes :

Keyname    Type   Unique  Packed  Column     Cardinality Collation  Null        
parent_id  BTREE  No      No      parent_id  1           A          YES 

Table : (comments)

表:(评论)

Column      Type        Null    Default Extra
id          int(11)     No      None    AUTO_INCREMENT      
parent_id   int(11)     Yes     NULL

Relation view:

Column     Foreign key constraint (INNODB)
parent_id  'test_site'.'comments'.'id'  ON DELETE CASCADE   ON UPDATE  NO ACTION

Is it possible to have parent_id not set to NULL. I have tried setting the default value to '0' and inserting the value '0' but I get the following error.

是否可以将parent_id设置为NULL。我已经尝试将默认值设置为'0'并插入值'0'但我收到以下错误。

Error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update 
a child row: a foreign key constraint fails (`test_site`.`comments`, 
CONSTRAINT `comments_ibfk_2` FOREIGN KEY (`parent_id`) REFERENCES `comments` 
(`id`) ON DELETE CASCADE ON UPDATE NO ACTION)

Any help on this would be much appreciated, Thank you.

对此有任何帮助将非常感谢,谢谢。

1 个解决方案

#1


1  

Yes, it is possible, although you have to circumvent the foreign key constraint just once to insert a dummy record for the default value. Here's my workflow:

是的,虽然您必须绕过外键约束一次才能插入默认值的虚拟记录,但这是可能的。这是我的工作流程:

Here's the table creation:

这是表创建:

root@localhost:playground > create table comments(id int auto_increment primary key, parent_id int not null default 0, constraint fk_parent_id foreign key (parent_id) references comments(id) on delete cascade on update cascade)engine=innodb;
Query OK, 0 rows affected (0.01 sec)

root@localhost:playground > show create table comments\G
*************************** 1. row ***************************
       Table: comments
Create Table: CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `fk_parent_id` (`parent_id`),
  CONSTRAINT `fk_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `comments` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

Now circumvent foreign key and insert dummy record.

现在规避外键并插入虚拟记录。

root@localhost:playground > set session foreign_key_checks=0;
Query OK, 0 rows affected (0.00 sec)

root@localhost:playground > insert into comments (id) values (null);                                                                                              Query OK, 1 row affected (0.00 sec)

root@localhost:playground > set session foreign_key_checks=1;
Query OK, 0 rows affected (0.00 sec)

root@localhost:playground > select * from comments;
+----+-----------+
| id | parent_id |
+----+-----------+
|  1 |         0 |
+----+-----------+
1 row in set (0.00 sec)

root@localhost:playground > update comments set id = 0 where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

root@localhost:playground > select * from comments;
+----+-----------+
| id | parent_id |
+----+-----------+
|  0 |         0 |
+----+-----------+
1 row in set (0.00 sec)

To make things neat and tidy I reset auto_increment (this is not necessary):

为了使事情干净整洁,我重置了auto_increment(这不是必需的):

root@localhost:playground > alter table comments auto_increment=0;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

And from now on your foreign key constraint is working properly and your column is no longer nullable and has a default value:

从现在开始,您的外键约束正常工作,您的列不再可以为空并且具有默认值:

root@localhost:playground > insert into comments (id) values (null);
Query OK, 1 row affected (0.00 sec)

root@localhost:playground > select * from comments;
+----+-----------+
| id | parent_id |
+----+-----------+
|  0 |         0 |
|  1 |         0 |
+----+-----------+
2 rows in set (0.00 sec)

#1


1  

Yes, it is possible, although you have to circumvent the foreign key constraint just once to insert a dummy record for the default value. Here's my workflow:

是的,虽然您必须绕过外键约束一次才能插入默认值的虚拟记录,但这是可能的。这是我的工作流程:

Here's the table creation:

这是表创建:

root@localhost:playground > create table comments(id int auto_increment primary key, parent_id int not null default 0, constraint fk_parent_id foreign key (parent_id) references comments(id) on delete cascade on update cascade)engine=innodb;
Query OK, 0 rows affected (0.01 sec)

root@localhost:playground > show create table comments\G
*************************** 1. row ***************************
       Table: comments
Create Table: CREATE TABLE `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `parent_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `fk_parent_id` (`parent_id`),
  CONSTRAINT `fk_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `comments` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

Now circumvent foreign key and insert dummy record.

现在规避外键并插入虚拟记录。

root@localhost:playground > set session foreign_key_checks=0;
Query OK, 0 rows affected (0.00 sec)

root@localhost:playground > insert into comments (id) values (null);                                                                                              Query OK, 1 row affected (0.00 sec)

root@localhost:playground > set session foreign_key_checks=1;
Query OK, 0 rows affected (0.00 sec)

root@localhost:playground > select * from comments;
+----+-----------+
| id | parent_id |
+----+-----------+
|  1 |         0 |
+----+-----------+
1 row in set (0.00 sec)

root@localhost:playground > update comments set id = 0 where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

root@localhost:playground > select * from comments;
+----+-----------+
| id | parent_id |
+----+-----------+
|  0 |         0 |
+----+-----------+
1 row in set (0.00 sec)

To make things neat and tidy I reset auto_increment (this is not necessary):

为了使事情干净整洁,我重置了auto_increment(这不是必需的):

root@localhost:playground > alter table comments auto_increment=0;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

And from now on your foreign key constraint is working properly and your column is no longer nullable and has a default value:

从现在开始,您的外键约束正常工作,您的列不再可以为空并且具有默认值:

root@localhost:playground > insert into comments (id) values (null);
Query OK, 1 row affected (0.00 sec)

root@localhost:playground > select * from comments;
+----+-----------+
| id | parent_id |
+----+-----------+
|  0 |         0 |
|  1 |         0 |
+----+-----------+
2 rows in set (0.00 sec)