SQLSTATE[HY000]:一般错误:1364 字段“contactId”没有默认值

Wij*_*and 4 php mysql laravel eloquent

在我的模型 ( Customer) 上,我有一个必需的参数:$contactId.

在执行时,Customer::create(['contactId' => $contactId])我收到以下错误:

"message": "SQLSTATE[HY000]: 一般错误: 1364 字段 'contactId' 没有默认值 (SQL: insert into customers( updated_at, created_at) values (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');
        });`
Run Code Online (Sandbox Code Playgroud)

是什么阻止Eloquent了在这里创建正确的插入语句?我已经三次检查了contactId整个流程和数据库中的拼写。

当我手动将一行插入到 中时customers,检索数据和contact-relation没有问题。

谢谢你的时间!

Dil*_*ara 7

$fillableCustomer模型中创建 contactId

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    protected $fillable = ['contactId'];
}
Run Code Online (Sandbox Code Playgroud)