首页 > 解决方案 > 无法使用 SQLSTATE 执行 PHP Artisan 迁移 [42000]

问题描述

我只想进行 PHP Artisan Migrate,我刚刚完成了我的迁移表但有错误

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'modified_date' 
(SQL: create table `dso` (`id` bigint unsigned not null auto_increment primary key, 
`id_dso` bigint unsigned not null, `id_rso` bigint unsigned not null, 
`id_focus` bigint unsigned not null, `id_wilayah` bigint unsigned not null, `id_grup_wilayah` bigint unsigned not null, 
`nama_dso` varchar(255) not null, `created_by` varchar(255) not null, 
`created_date` timestamp not null, `modified_by` varchar(255) not null, 
`modified_date` timestamp not null, `status` tinyint(1) not null, 
`created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

我检查了我的完成情况,没有错误或任何拼写错误

public function up()
    {
        Schema::create('dso', function (Blueprint $table) {
            $table->id();
            $table->foreignId('id_dso')->constrained('dso_table_name');
            $table->foreignId('id_rso')->constrained('rso_table_name');
            $table->foreignId('id_focus')->constrained('focus_table_name');
            $table->foreignId('id_wilayah')->constrained('wilayah_table_name');
            $table->foreignId('id_grup_wilayah')->constrained('grup_wilayah_table_name');
            $table->string('nama_dso');
            $table->string('created_by');
            $table->timestamp('created_date',$precision = 0);
            $table->string('modified_by');
            $table->timestamp('modified_date',$precision = 0);
            $table->boolean('status');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('dso');
    }
}

我没有添加可空值,因为所有列都必须填充我正在使用 Laravel 8.6,在 Windows 上使用 XAMPP 和 MariaDB

标签: phplaravelmigrationlaravel-8

解决方案


您的问题是您使用timestamps()而不是timestamp().

所以,你的迁移应该是这样的:

public function up()
{
    Schema::create('dso', function (Blueprint $table) {
        $table->id();
        $table->foreignId('id_dso')->unique();
        $table->id('id_rso');
        $table->id('id_focus');
        $table->id('id_wilayah');
        $table->id('id_grup_wilayah');
        $table->string('nama_dso');
        $table->string('created_by');
        $table->timestamp('created_date');
        $table->string('modified_by');
        $table->timestamp('modified_date');
        $table->boolean('status');
        $table->timestamps();
    });
}

检查文档中的可用方法。


推荐阅读