获取语法错误或访问冲突:1067 laravel 中 'deadline' 的默认值无效迁移
getting Syntax error or access violation: 1067 Invalid default value for 'deadline' in laravel migrate
我正在迁移 table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateExamsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('exams', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->enum('type',['iq','math','geo','gen']);
$table->string('notes');
$table->boolean('vip');
$table->string('rate');
$table->integer('hardness');
$table->string('quode');
$table->timestamp('bornline');
$table->timestamp('deadline', $precision = '0000-00-00');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('exams');
}
}
cmd 返回此错误
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid
default value for 'deadline' (SQL: create table exams
(id
bigint
unsigned not null auto_increment primary key, name
varchar(191) not
null, type
enum('iq', 'math', 'geo', 'gen') not null, notes
varchar(191) not null, vip
tinyint(1) not null, rate
varchar(191)
not null, hardness
int not null, quode
varchar(191) not null,
bornline
timestamp not null, deadline
timestamp not null,
created_at
timestamp null, updated_at
timestamp null) default
character set utf8mb4 collate 'utf8mb4_unicode_ci')
at
C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:705
701▕ // If an exception occurs when attempting to run a query, we'll format the error
702▕ // message to include the bindings with SQL, which will make this exception a
703▕ // lot more helpful to the developer instead of just the database's errors.
704▕ catch (Exception $e) { ➜ 705▕ throw new QueryException(
706▕ $query, $this->prepareBindings($bindings), $e
707▕ );
708▕ }
709▕ }
C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:494
PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'deadline'")
C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:494
PDOStatement::execute()
小数秒精度 (fsp) 没有像 0000-00-00
这样的格式。 fsp 值,如果给定, 必须在 0
到 6
.
范围内
使用 0000-00-00
格式,我相信您想为 deadline
列创建一个默认值。您可以使用 default()
方法:
$table->timestamp('deadline')->default('0000-00-00');
或nullable()
方法,允许将NULL
值输入到列中:
$table->timestamp('deadline')->nullable();
我正在迁移 table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateExamsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('exams', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->enum('type',['iq','math','geo','gen']);
$table->string('notes');
$table->boolean('vip');
$table->string('rate');
$table->integer('hardness');
$table->string('quode');
$table->timestamp('bornline');
$table->timestamp('deadline', $precision = '0000-00-00');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('exams');
}
}
cmd 返回此错误
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'deadline' (SQL: create table
exams
(id
bigint unsigned not null auto_increment primary key,name
varchar(191) not null,type
enum('iq', 'math', 'geo', 'gen') not null,notes
varchar(191) not null,vip
tinyint(1) not null,rate
varchar(191) not null,hardness
int not null,quode
varchar(191) not null,bornline
timestamp not null,deadline
timestamp not null,created_at
timestamp null,updated_at
timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')at C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:705
701▕ // If an exception occurs when attempting to run a query, we'll format the error
702▕ // message to include the bindings with SQL, which will make this exception a
703▕ // lot more helpful to the developer instead of just the database's errors.
704▕ catch (Exception $e) { ➜ 705▕ throw new QueryException(
706▕ $query, $this->prepareBindings($bindings), $e
707▕ );
708▕ }
709▕ }C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:494 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'deadline'")
C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:494 PDOStatement::execute()
小数秒精度 (fsp) 没有像 0000-00-00
这样的格式。 fsp 值,如果给定, 必须在 0
到 6
.
使用 0000-00-00
格式,我相信您想为 deadline
列创建一个默认值。您可以使用 default()
方法:
$table->timestamp('deadline')->default('0000-00-00');
或nullable()
方法,允许将NULL
值输入到列中:
$table->timestamp('deadline')->nullable();