gpt4 book ai didi

mysql - 无法添加或更新子行: a foreign key constraint fails (Laravel)

转载 作者:行者123 更新时间:2023-11-29 19:06:17 25 4
gpt4 key购买 nike

我有错误,并且我不知道如何正确处理 Eloquent 关系:c类别编号正确,但用户 ID 错误。我认为它是从表 models.id 中获取的,但需要 models.user_id这是我的表格:

photoset_categories(包含照片集类型的目录,例如: id = 1,name = 'Studio';id = 2,name = 'Portrait')

Schema::create('photoset_categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->comment('Category name');
});

模特(模特数据:眼睛颜色、高度、体重等)

Schema::create('models', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->comment('Model id from users');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); // Foreign key

$table->timestamps();
});

model_photosets(拍摄模特的照片)

Schema::create('model_photosets', function (Blueprint $table) {
$table->increments('id');
$table->integer('model_id')->unsigned()->index();
$table->foreign('model_id')->references('user_id')->on('models')->onDelete('cascade'); // Foreign key
$table->integer('category_id')->unsigned()->index();
$table->foreign('category_id')->references('id')->on('photoset_categories')->onDelete('cascade'); // Foreign key
});

以下是数据库模型:

class PhotosetCategory extends Model
{
protected $table = 'photoset_categories';
protected $guarded = ['name'];

public function modelPhotoset()
{
return $this->belongsToMany('App\Models');
}
}

...

class Models extends Model
{
protected $table = 'models';
protected $fillable = ['user_id', 'd_birth', 'birth', 'm_birth', 'y_birth', 'hair_color', 'eyes_color', 'growth', 'weight'];
protected $dates = ['created_at', 'updated_at'];


public function user() {
return $this->belongsToMany('App\User');
}

public function photosets()
{
return $this->belongsToMany('App\PhotosetCategory', 'model_photosets', 'model_id', 'category_id');
}
}

...

class ModelPhotoset extends Model
{
protected $table = 'model_photosets';
protected $fillable = ['user_id', 'category_id'];
}

Controller 模型 Controller

class ModelsController extends Controller
{
public function editData(Request $request)
{
$title = 'Model data';

$data = Models::where('user_id', Auth::id())->first();

$all_types = PhotosetCategory::all(); // List of photo shoot catagories

if ($request->has('save')) {
$this->validate($request, [
// ...
]);

// UPDATE USER PRO SHOOTS DATA
$data->photosets()->sync($request->get('ps_types'));
}

return view('model.edit', [
'title' => $title,
'data' => $data,
]);
}
}

错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (delavour.model_photosets, CONSTRAINT model_photosets_model_id_foreign FOREIGN KEY (model_id) REFERENCES models (user_id) ON DELETE CASCADE) (SQL: insert into model_photosets (category_id, model_id) values (2, 2))

最佳答案

错误表明您正在尝试插入 model_id 为 2 的 model_photosets,但在 id 为 2 的模型中记录不会退出。

关于mysql - 无法添加或更新子行: a foreign key constraint fails (Laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43478791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com