我的迁移在本地工作,但在生产上失败,错误:
Migrating: 2024_03_19_145356_tasks
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'send_before' (SQL: create table `tasks` (`id` bigint unsigned not null auto_increment primary key, `subscription_id` bigint unsigned not null, `send_after` timestamp not null, `send_before` timestamp not null, `sent_at` timestamp null comment 'null = not sent', `failed_at` timestamp null, `delivered` tinyint(1) null default '0' comment '0 - not delivered, 1 - delivered, null - unknown', `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
我的调试失败得很惨,因为SQL是有效的,事实上,当通过phpMyAdmin或终端运行时,它在生产环境中运行得很好:
create table `tasks` (`id` bigint unsigned not null auto_increment primary key, `subscription_id` bigint unsigned not null, `send_after` timestamp not null, `send_before` timestamp not null, `sent_at` timestamp null comment 'null = not sent', `failed_at` timestamp null, `delivered` tinyint(1) null default '0' comment '0 - not delivered, 1 - delivered, null - unknown', `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
2024_03_19_145356_tasks.php个
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Tasks extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->foreignId('subscription_id')->constrained('subscriptions')->cascadeOnDelete();
$table->timestamp('send_after')->index();
$table->timestamp('send_before')->index();
$table->timestamp('sent_at')->nullable()->index()->comment('null = not sent');
$table->timestamp('failed_at')->nullable();
$table->boolean('delivered')->default(0)->comment('0 - not delivered, 1 - delivered, null - unknown')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}
服务器版本:5.5.60-MariaDB MariaDB服务器
PHP 7.4.33(版本)(built:Dec 12 2023 14:45:16)(NTS)
Laravel框架8.83.27
UPDATED 1
将dump(\DB::select("SELECT @@version, @@sql_mode"));
添加到迁移中,我发现迁移过程中的sql_mode
具有以下sql_mode
设置:
array:1 [
0 => {#1958
+"@@version": "5.5.60-MariaDB"
+"@@sql_mode": "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
}
]
UPDATED (SOLVED?)
我找到了问题所在.我不想假装我知道发生了什么,所以我会如实地说:
当迁移具有多于一个列类型timestamp
和is not nullable
时,问题就会出现.以下是一些例子:
作品:
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->timestamp('test1');
$table->timestamp('test2')->nullable();
$table->timestamp('test3')->nullable();
});
错误1067 Invalid default value for 'test2'
:
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->timestamp('test1');
$table->timestamp('test2');
$table->timestamp('test3')->nullable();
});
作品:
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->timestamp('test1')->nullable();
$table->timestamp('test2')->nullable();
$table->timestamp('test3');
});
错误1067 Invalid default value for 'test2'
:
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->timestamp('test1');
$table->timestamp('test2');
});
作品:
Schema::create('tasks', function (Blueprint $table) {
$table->id();
$table->timestamp('test1')->nullable();
$table->timestamp('test2');
$table->timestamp('test3')->nullable();
});
而且...如果我用dateTime
,它总是工作.所以...这个问题已经解决了,虽然我不知道发生了什么,我想知道,