簡體   English   中英

SQLSTATE[HY000]:一般錯誤:1364 字段“contactId”沒有默認值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM