【发布时间】:2017-10-08 06:05:53
【问题描述】:
当我单击提交(保存)按钮进行作业时...然后出现以下异常...(user_name 字段已在 job.create 中设置为隐藏。刀片查看表单)
您能告诉我在哪里可以修改以进行修复吗? 谢谢。
(3/3) 查询异常
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) 值 (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);
}
【问题讨论】: