微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Laravel - SQLSTATE[42000]:语法错误或访问冲突:1067 无效的默认值

如何解决Laravel - SQLSTATE[42000]:语法错误或访问冲突:1067 无效的默认值

我正在创建一个带有 2 个时间戳的迁移,但由于某种原因,我不能在没有 timestamps/default 值的情况下放置 2 个 nullable

我的代码

        Schema::create('lessons',function (Blueprint $table) {
            $table->id();
            $table->timestamp('checkin');
            $table->timestamp('checkout');
            $table->string('url')->nullable();
            $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
            $table->softDeletes();
        });

如果我在 checkout: $table->timestamp('checkout')->nullable; 中放置了一个可为空的值,我的代码工作正常。但是我的两个值不能为空。

错误

 Illuminate\Database\QueryException 

  sqlSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'checkout' (sql: create table `lessons` (`id` bigint unsigned not null auto_increment primary key,`checkin` timestamp not null,`checkout` timestamp not null,`url` varchar(191) null,`created_at` timestamp not null default CURRENT_TIMESTAMP,`updated_at` timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,`deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

如果更改顺序,错误也会发生在签入时

sqlSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'checkin' (sql: create table `lessons` (`id` bigint unsigned not null auto_increment primary key,`deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

我认为这可能是 Laravel 中的一个错误

  • 注意 1:如果我删除了不可为空的“第二个”时间戳,那就没问题了。
  • 注意 2:我使用 PHP artisan migration:fresh 来获得这些结果。

解决方法

我用这篇文章解决了我的问题: https://stackoverflow.com/a/59326426/11639058

我已将 timestamp 更改为 dateTime

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。