[英]SQLSTATE[HY000]: General error: 1364 Field 'photo' doesn't have a default value in laravel 5.5
[英]SQLSTATE[HY000]: General error: 1364 Field 'contactId' doesn't have a default value
在我的模型 ( Customer
) 上,我有一個必需的參數: $contactId
。
在執行Customer::create(['contactId' => $contactId])
我收到以下錯誤:
"message": "SQLSTATE[HY000]: 一般錯誤: 1364 字段 'contactId' 沒有默認值 (SQL: insert into
customers
(updated_at
,created_at
) 值 (2019-12-10 12:33:46, 2019) -12-10 12:33:46))"
在插入語句中找不到 contactId 字段。 contactId
的值設置為4
,它通過FK
對應於數據庫中的正確值。
Customer
表的遷移:
`Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
$table->bigInteger('contactId')->unsigned();
$table->foreign('contactId', 'FK_customer_contactId_contact_id')
->references('id')
->on('contacts');
});`
是什么阻止了Eloquent
在這里創建正確的插入語句? 我已經在整個流程和數據庫中三重檢查了contactId
的拼寫。
當我手動將一行插入到customers
,檢索數據和contact
關系沒有問題。
謝謝你的時間!
在Customer
模型中使 contactId $fillable
fillable
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = ['contactId'];
}
如果您使用create
方法將數據保存到數據庫中,則必須在模型中編寫此fillable
屬性
<?php
class Customer extends Model
{
protected $fillable = ['contactId'];
}
在模型中使 contactId $fillable
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = ['contactId'];
}
或者
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $guarded = [];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.