当我尝试将数据添加到children
表时。
我犯了以下错误。
我试着找出原因。。
插入数据是否有任何错误之处?
如果有人有意见,请告诉我
感谢
QueryFailedError: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`mogucare_db`.`children`, CONSTRAINT `FK_644df4cc525729181dbee86a44d` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
以下是我对DB的看法。
mysql> select * from children;
Empty set (0.00 sec)
mysql> select * from users;
Empty set (0.00 sec)
mysql> desc children;
+--------------+--------------+------+-----+----------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+----------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| mogu_plus_id | varchar(255) | NO | | NULL | |
| first_name | varchar(255) | NO | | NULL | |
| middle_name | varchar(255) | YES | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| birthday | date | NO | | NULL | |
| gender | varchar(255) | NO | | NULL | |
| blood_type | varchar(255) | NO | | NULL | |
| class | varchar(255) | YES | | NULL | |
| entrance_day | varchar(255) | YES | | NULL | |
| is_active | int(11) | NO | | NULL | |
| created_at | datetime(6) | NO | | CURRENT_TIMESTAMP(6) | |
| updated_at | datetime(6) | NO | | CURRENT_TIMESTAMP(6) | |
| deleted_at | datetime(6) | YES | | NULL | |
+--------------+--------------+------+-----+----------------------+----------------+
15 rows in set (0.01 sec)
mysql> desc users;
+-------------------+--------------+------+-----+----------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+----------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| password | varchar(255) | NO | | NULL | |
| birthday | date | NO | | NULL | |
| address | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | | NULL | |
| gender | varchar(255) | NO | | NULL | |
| height | varchar(255) | NO | | NULL | |
| weight | varchar(255) | NO | | NULL | |
| phone | varchar(255) | NO | | NULL | |
| login_id | varchar(255) | NO | | NULL | |
| first_name | varchar(255) | NO | | NULL | |
| middle_name | varchar(255) | YES | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| blood_type | varchar(255) | NO | | NULL | |
| language_cd | int(11) | NO | | NULL | |
| emergency_contact | varchar(255) | YES | | NULL | |
| is_admin | int(11) | NO | | NULL | |
| created_at | datetime(6) | NO | | CURRENT_TIMESTAMP(6) | |
| updated_at | datetime(6) | NO | | CURRENT_TIMESTAMP(6) | |
| deleted_at | datetime(6) | YES | | NULL | |
+-------------------+--------------+------+-----+----------------------+----------------+
20 rows in set (0.00 sec)
查看您的错误,在我看来,表Children在字段User_Id上有一个外键,该外键需要与表Users中现有的Id相匹配
因此,一切都正常工作,如果没有对表Users中的行的有效引用,就不能在表Children的行中输入。
因此,首先在表Users中创建一行或多行,然后可以在表Children创建一行,该行引用表Users中的一行