如何解决字段X没有默认值?

时间:2019-09-08 08:20:08

标签: mysql laravel

我的函数中有这段代码旨在更新或创建它的子类,这就是它的外观:

$tr_routes = json_decode($request['tr_routes'], true);
if(count($tr_routes) > 0){
    foreach ($tr_routes as $tr_route) {
        if(empty($tr_route['id'])){
            $tr_route['tr_id'] = $tr_id;
            unset($tr_route['id']);
            TRequestRoutes::create($tr_route);
        }else{
            TRequestRoutes::update($tr_route);
        }
    }
}

我现在的问题是它给了我这个错误:

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `trequest_routes` (`routes_name`, `class`, `date_request`, `time`, `tr_id`, `updated_at`, `created_at`) values (asdasd, asd, asd, 2019-12-12, , 6, 2019-09-08 08:02:28, 2019-09-08 08:02:28))

我想知道'id'的来源是什么,而实际上插入语句ID上还没有?如果我在$ tr_route字段中输出了ID,则该ID在此处成功设置。

1 个答案:

答案 0 :(得分:3)

您的ID字段似乎未设置为“自动增量”。您可以更改迁移文件并将ID设置为在此处递增:

    Schema::create('tableName', function (Blueprint $table) {
        $table->increments('id');
        $table->string('yourField');
        $table->string('anotherField');
    });

然后再次迁移。