英文:
Constraint error when updating m2m relationship
问题
// 以下是要翻译的内容:
我在模型中建立了与连接表的多对多关系,如下所示:
public function users()
{
return $this->belongsToMany(User::class, 'user_alert', 'alert_id', 'user_id');
}
public function alerts()
{
return $this->belongsToMany(Alert::class, 'user_alert', 'user_id', 'alert_id');
}
当使用这行编辑警报时,我收到以下错误消息。
Alert::findOrFail($request->id)->users()->syncWithoutDetaching($users);
SQLSTATE[23000]: 完整性约束违规: 1452 无法添加或更新子行: 外键约束失败 (homestead
.user_alert
, CONSTRAINT user_alert_user_id_foreign
FOREIGN KEY (user_id
) REFERENCES users
(id
) ON DELETE CASCADE) (SQL: insert into user_alert
(alert_id
, id
, user_id
) values (43, 1, 0))
user_id 参数为 0 导致了错误,我不知道它是从哪里来的。执行 `dd($users)` 显示类似以下内容:
array:5 [
0 => array:1 [
"id" => 1
]
1 => array:1 [
"id" => 1
]
2 => array:1 [
"id" => 2
]
]
0 首次出现在调用 `/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php` 中。
"file" => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php",
"line" => 254,
"function" => "insert",
"class" => "Illuminate\Database\Query\Builder",
"type" => "->",
"args" => array:1 [
0 => & array:1 [
0 => array:3 [
"alert_id" => 43,
"id" => 1,
"user_id" => 0
]
]
]
]
令人沮丧的是,在创建新警报时,多对多关系能够正常工作。
我希望这并不太重要,但这是 Laravel 7。
希望有人能提供一些建议!
英文:
I have a M2M relationship with a junction table, set up in the models as:
public function users()
{
return $this->belongsToMany(User::class, 'user_alert', 'alert_id', 'user_id');
}
public function alerts()
{
return $this->belongsToMany(Alert::class, 'user_alert', 'user_id', 'alert_id');
}
When editing an alert with this line, I get the following error.
Alert::findOrFail($request->id)->users()->syncWithoutDetaching($users);
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`homestead`.`user_alert`, CONSTRAINT `user_alert_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) (SQL: insert into `user_alert` (`alert_id`, `id`, `user_id`) values (43, 1, 0))
The user_id argument 0 is causing the error and I have no idea where it's coming from. Doing a dd($users)
shows something similar to:
array:5 [
0 => array:1 [
"id" => 1
]
1 => array:1 [
"id" => 1
]
2 => array:1 [
"id" => 2
]
]
The 0 first appears in a call to /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php
"file" => "/var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php"
"line" => 254
"function" => "insert"
"class" => "Illuminate\Database\Query\Builder"
"type" => "->"
"args" => array:1 [▼
0 => & array:1 [▼
0 => array:3 [▼
"alert_id" => 43
"id" => 1
"user_id" => 0
]
]
]
]
Frustratingly, the M2M relationship works when creating a new alert.
I hope it's not too relevant, but this is Laravel 7.
Hope somebody has some advice!
答案1
得分: 1
问题出在$users数据的格式上。syncWithoutDetaching
可以接受用户模型的集合或用户ID的数组。由于您提供的格式不受支持,因为它将数组键(在您的示例中为0, 1, 2)视为用户ID。
因此,请使用用户模型的集合或用户ID的数组来提供syncWithoutDetaching
。
英文:
Problem is in the format of the $users data. syncWithoutDetaching
can take collection of User models or array of user ids. Format you provided is not supported because takes array keys (0,1,2 in your example) as user ids.
So please provide syncWithoutDetaching
with collection of User models or array of user ids.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论