gpt4 book ai didi

php - SQLSTATE[HY000] : General error: 1364 Field 'contactId' doesn't have a default value

转载 作者:行者123 更新时间:2023-12-02 10:58:50 24 4
gpt4 key购买 nike

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

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

"message": "SQLSTATE[HY000]: General error: 1364 Field 'contactId' doesn't have a default value (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');
});`

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

当我手动向 customers 中插入一行时,检索数据和 contact 关系没有任何问题。

感谢您的宝贵时间!

最佳答案

Customer 模型中使 contactId $fillable

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
protected $fillable = ['contactId'];
}

关于php - SQLSTATE[HY000] : General error: 1364 Field 'contactId' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59267758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com