![](/img/trans.png)
[英]SQLSTATE[23000]: Integrity constraint violation: in Laravel 5.2
[英]SQLSTATE[23000]: Integrity constraint violation in Laravel
当我单击提交(保存)按钮以创建作业时...然后出现以下异常...。( user_name字段已在job.create.blade视图表单中隐藏为已放置 )
您能告诉我在哪里可以修改以进行修复? 谢谢。
(3/3)QueryException
SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键约束失败(
admin-db
。jobs
,约束jobs_customer_name_foreign
外键(customer_name
)参考文献customers
(customer_name
))(SQL:插入到jobs
(user_name
,customer_name
,job_place
,job_type
,note_1
,time_used
,updated_at
,created_at
)值(约翰,1,KONTOR,多梅内OG Webhotell,ASDF,2017年10月8日0时23分40秒,2017年10月8日0时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);
}
我一个人得到答案...
问题是采摘方法 。 修改后,下面的工作正常。
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.