【问题标题】:getting Syntax error or access violation: 1067 Invalid default value for 'deadline' in laravel migrate获取语​​法错误或访问冲突:1067 laravel migrate 中“deadline”的默认值无效
【发布时间】:2022-01-23 11:55:38
【问题描述】:

我正在迁移表:

<?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

照亮\数据库\查询异常

SQLSTATE[42000]:语法错误或访问冲突:1067 无效 'deadline' 的默认值 (SQL: create table exams (id bigint unsigned not null auto_increment 主键,name varchar(191) 不是 null, type enum('iq', 'math', 'geo', 'gen') not null, notes varchar(191) 不为空,vip tinyint(1) 不为空,ratevarchar(191) 不为空,hardness int 不为空,quode varchar(191) 不为空, bornline 时间戳不为空,deadline 时间戳不为空, created_attimestamp null, updated_attimestamp null) 默认 字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')

在 C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:705
701▕ // 如果尝试运行查询时发生异常,我们将格式化错误
702▕ // 包含与 SQL 的绑定的消息,这将使此异常成为一个
703▕ // 对开发人员更有帮助,而不仅仅是数据库的错误。
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]: 语法错误或访问冲突:1067 'deadline' 的默认值无效")

C:\Users\xxgam\OneDrive\Desktop\projects\deneme\vendor\laravel\framework\src\Illuminate\Database\Connection.php:494 PDOStatement::execute()

【问题讨论】:

  • 我从 laravel 创建它我无法修改 SQL 语法
  • 应该是$table-&gt;timestamp('deadline', $precision = 0);
  • 语法错误或访问冲突:1067 'deadline' 的默认值无效
  • '0000-00-00' 不允许

标签: mysql laravel migration database-migration


【解决方案1】:

小数秒精度 (fsp) 没有像 0000-00-00 这样的格式。 fsp 值(如果给定)必须在 06 的范围内

使用0000-00-00 格式,相信您想为deadline 列创建一个默认值。您可以使用default() 方法:

$table->timestamp('deadline')->default('0000-00-00');

nullable() 方法,允许将NULL 值输入到列中:

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

【讨论】:

    猜你喜欢
    • 2021-04-15
    • 2018-02-06
    • 2021-07-21
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    相关资源
    最近更新 更多