Search code examples
laravellaravel-migrations

Why creating table in migration error with created_at table?


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 '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')

Migration file have :

<?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');
    }
};

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!


Solution

  • 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->timestamps();
    

    See documentation:
    Laravel migrations: timestamps()