當我點擊提交(保存)按鈕以創建一個Job ...然後我得到下面的異常....(user_name field已被假定爲隱藏在作業中.create.blade查看錶單)SQLSTATE [23000]:Laravel中的完整性約束違規
你能告訴我在哪裏修改修復? 謝謝。
(3/3)QueryException
SQLSTATE [23000]:完整性約束違規:1452不能添加或更新 子行:外鍵約束失敗(
admin-db
。jobs
, CONSTRAINTjobs_customer_name_foreign
FOREIGN KEY(customer_name
) REFERENCEScustomers
(customer_name
))(SQL:insert intojobs
(user_name
,customer_name
,job_place
,job_type
,note_1
,time_used
,updated_at
,created_at
)值(約翰,1,KONTOR, 多梅內OG Webhotell,ASDF,2017年10月8日零時二十三分40秒,2017-10 -08 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.ph p
public function jobs()
{
return $this->hasMany(Job::class);
}