约束错误在更新 m2m 关系时发生。

huangapple go评论58阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月26日 11:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定