英文:
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 '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!
答案1
得分: 1
使用Laravel的timestamps()
方法
Laravel内置了一个方法来为您创建这些列:timestamps()
。
将会为您创建created_at
和updated_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->timestamps();
See documentation:
Laravel migrations: timestamps()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论