【问题标题】:Laravel - SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default valueLaravel - SQLSTATE [42000]:语法错误或访问冲突:1067 默认值无效
【发布时间】:2021-04-15 06:41:52
【问题描述】:

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

我的代码:

        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, `checkout` timestamp not null, `checkin` 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')

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

  • 注意 1:如果我删除不可为空的“第二个”时间戳,则可以正常工作。
  • 注意 2:我使用 php artisan migration:fresh 来获得这些结果。

【问题讨论】:

    标签: eloquent laravel-8 laravel-migrations php-7.4


    【解决方案1】:

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

    我已将timestamp 更改为dateTime

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 2018-02-06
      • 2023-04-01
      • 1970-01-01
      • 2017-07-31
      • 2017-10-29
      • 2019-06-06
      • 1970-01-01
      • 2015-09-20
      相关资源
      最近更新 更多