gpt4 book ai didi

mysql - SQLSTATE[23000] : Integrity constraint violation in Laravel

转载 作者:行者123 更新时间:2023-11-29 10:34:52 33 4
gpt4 key购买 nike

当我单击提交(保存)按钮来创建作业时...然后我遇到了以下异常...(user_name 字段已隐藏在 job.create.txt 中。 Blade View 形式)

你能告诉我哪里可以修改修复吗?谢谢。

enter image description here

(3/3) 查询异常

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (admin-db.jobs, CONSTRAINT jobs_customer_name_foreign FOREIGN KEY (customer_name) REFERENCES customers (customer_name)) (SQL: insert into jobs (user_name, customer_name, job_place, job_type, note_1, time_used, updated_at, created_at) values (John, 1, Kontor, Domene og Webhotell, asdf, , 2017-10-08 00:23:40, 2017-10-08 00:23:40))

timestamp_create_users_table.php

public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id')->unsigned();

$table->string('name')->index();
$table->string('email');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

timestamp_create_customers_table.php

public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('id')->unsigned();

$table->string('customer_name')->index();
$table->string('customer_email');
$table->timestamps();
$table->softDeletes();
});
}

timestamp_create_jobs_table.php

public function up()
{

Schema::create('jobs', function (Blueprint $table) {
$table->engine = 'InnoDB';

$table->increments('id')->unsigned();

$table->string('user_name')->index();
$table->string('customer_name')->index();
$table->string('job_place');
$table->string('job_type');
$table->string('note_1');
$table->time('time_used')->nullable();
$table->timestamps();
$table->softDeletes();

$table->foreign('user_name')->nullable()->references('name')->on('users')->onDelete('cascade');
$table->foreign('customer_name')->nullable()->references('customer_name')->on('customers')->onDelete('cascade');
});
}

模型关系定义在:Job.php

public function users()
{
return $this->belongsTo(User::class);
}

public function customers()
{
return $this->belongsTo(Customer::class);
}

模型关系定义在:Customer.php

public function jobs()
{
return $this->hasMany(Job::class);
}

最佳答案

我自己得到了答案...

问题出在pluck方法中使用。下面修改后运行良好。

<div class="form-group col-sm-6">
{!! Form::label('customer_name', 'Customer Name:') !!}
{!! Form::select('customer_name', $searchdata->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!}
</div>

关于mysql - SQLSTATE[23000] : Integrity constraint violation in Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46628037/

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