为什么在迁移中创建带有 created_at 字段的表会出错?

huangapple go评论96阅读模式
英文:

Why creating table in migration error with created_at table?

问题

在远程服务器上使用 Ubuntu 18.04 上传 Laravel 9 项目时,我遇到了迁移错误:

2023_01_18_053125_create_user_meetings_table ............................................................................................ 1ms FAIL

Illuminate\Database\QueryException

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'created_at' (SQL: create table `user_meetings` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `user_name` varchar(100) not null, `user_email` varchar(100) not null, `user_quiz_request_id` bigint unsigned not null, `appointed_at` timestamp null, `status` enum('W', 'A', 'M', 'C', 'D') not null comment 'W => Waiting for review, A => Accepted for meeting, M => Marked for future contacts, C => Cancelled, D-Declined', `created_at` timestamp not null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

迁移文件内容如下:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_meetings', function (Blueprint $table) {
            $table->id();

            $table->string('name', 255);
            $table->string('user_name', 100);
            $table->string('user_email', 100);

            $table->bigInteger('user_quiz_request_id')->unsigned();
            $table->foreign('user_quiz_request_id', 'user_meetings_user_quiz_request_id_foreign')->references('id')->on('user_quiz_requests')->onUpdate('RESTRICT')->onDelete('CASCADE');


            $table->timestamp('appointed_at')->nullable();

            $table->enum('status', ['W', 'A', 'M', 'C', 'D'])->comment('W => Waiting for review, A => Accepted for meeting, M => Marked for future contacts, C => Cancelled, D-Declined');

            $table->timestamp('created_at');
            $table->timestamp('updated_at')->nullable();
            $table->index(['status', 'user_email', 'appointed_at'], 'user_meetings_status_user_email_appointed_at_index');
            $table->index(['user_quiz_request_id', 'user_email', 'status'], 'user_meetings_user_quiz_request_id_user_email_status_index');
        });
    }

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

我使用以下选项创建了数据库:

quizzes utf8_general_ci

我想知道在错误消息中提到的 "utf8mb4_unicode_ci" 是从哪里来的,因为我使用 "utf8_general_ci" 选项创建了数据库?

此外,通过 phpMyAdmin 检查创建的表,参见:链接

为什么会出现错误,因为这是表创建语句,而不是添加行?

谢谢!

英文:

Uploading laravel 9 project on remote server with ubuntu 18.04 I got error migrating:

2023_01_18_053125_create_user_meetings_table ............................................................................................ 1ms FAIL

Illuminate\Database\QueryException

SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for &#39;created_at&#39; (SQL: create table `user_meetings` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `user_name` varchar(100) not null, `user_email` varchar(100) not null, `user_quiz_request_id` bigint unsigned not null, `appointed_at` timestamp null, `status` enum(&#39;W&#39;, &#39;A&#39;, &#39;M&#39;, &#39;C&#39;, &#39;D&#39;) not null comment &#39;W =&gt; Waiting for review,  A =&gt; Accepted for meeting, M=&gt;Marked for future contacts, C=&gt;Cancelled, D-Declined&#39;, `created_at` timestamp not null, `updated_at` timestamp null) default character set utf8mb4 collate &#39;utf8mb4_unicode_ci&#39;)

Migration file have :

&lt;?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create(&#39;user_meetings&#39;, function (Blueprint $table) {
            $table-&gt;id();

            $table-&gt;string(&#39;name&#39;, 255);
            $table-&gt;string(&#39;user_name&#39;, 100);
            $table-&gt;string(&#39;user_email&#39;, 100);

            $table-&gt;bigInteger(&#39;user_quiz_request_id&#39;)-&gt;unsigned();
            $table-&gt;foreign(&#39;user_quiz_request_id&#39;, &#39;user_meetings_user_quiz_request_id_foreign&#39;)-&gt;references(&#39;id&#39;)-&gt;on(&#39;user_quiz_requests&#39;)-&gt;onUpdate(&#39;RESTRICT&#39;)-&gt;onDelete(&#39;CASCADE&#39;);


            $table-&gt;timestamp(&#39;appointed_at&#39;)-&gt;nullable();

            $table-&gt;enum(&#39;status&#39;, [&#39;W&#39;, &#39;A&#39;, &#39;M&#39;, &#39;C&#39;, &#39;D&#39;])-&gt;comment(&#39;W =&gt; Waiting for review,  A =&gt; Accepted for meeting, M=&gt;Marked for future contacts, C=&gt;Cancelled, D-Declined&#39;);

            $table-&gt;timestamp(&#39;created_at&#39;);
            $table-&gt;timestamp(&#39;updated_at&#39;)-&gt;nullable();
            $table-&gt;index([&#39;status&#39;, &#39;user_email&#39;, &#39;appointed_at&#39;], &#39;user_meetings_status_user_email_appointed_at_index&#39;);
            $table-&gt;index([&#39;user_quiz_request_id&#39;, &#39;user_email&#39;, &#39;status&#39;], &#39;user_meetings_user_quiz_request_id_user_email_status_index&#39;);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists(&#39;user_meetings&#39;);
    }
};

I created the database with option :

quizzes	utf8_general_ci

I wonder where from in error message "utf8mb4_unicode_ci" is mentioned, as I created database with "utf8_general_ci" option ?

Also checking created tables in phpmyadmin see : https://prnt.sc/U53Vi-vKW8oz

Why error, as it is table creating statement, not adding row ?

Thanks!

答案1

得分: 1

使用Laravel的timestamps()方法

Laravel内置了一个方法来为您创建这些列:timestamps()
将会为您创建created_atupdated_at列。

在迁移中,您可以像这样调用它:

$table->timestamps();

查看文档:
Laravel 迁移:timestamps()

英文:

Use Laravel's timestamps() method

Laravel has a built-in method to create these columns for you: timestamps().
The created_at and updated_at columns will be created for you.

You call it like this in the migration:

$table-&gt;timestamps();

See documentation:
Laravel migrations: timestamps()

huangapple
  • 本文由 发表于 2023年2月14日 20:42:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定