【问题标题】:Laravel Migration SQLSTATE[42000]: Syntax error or access violation: 1064Laravel 迁移 SQLSTATE[42000]:语法错误或访问冲突:1064
【发布时间】:2020-01-17 20:46:29
【问题描述】:

对于一个非常旧的迁移(过去运行良好),我遇到了一个新的迁移错误。

我得到的错误是:SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`' at line 1 (SQL: ALTER TABLE rooms CHANGE conversion conversion TINYINT(1) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`)

迁移文件如下所示:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class ChangeRoomsConversionToBoolean extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('rooms', function (Blueprint $table) {
            $table->boolean('conversion')->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('rooms', function (Blueprint $table) {
            $table->string('conversion')->change();
        });
    }
}

如果我直接在数据库ALTER TABLE rooms CHANGE conversion conversion TINYINT(1) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci` 中运行查询,则会收到错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8 NOT NULL' at line 1

我正在使用 Laravel 5.6 在 Homestead 上运行。

任何帮助将不胜感激。

【问题讨论】:

    标签: php mysql laravel laravel-5.6


    【解决方案1】:

    我相信有一些issues with how Laravel is configuring DBAL;但是,我认为以下内容将解决您的问题:

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::table('rooms', function (Blueprint $table) {
                $table->boolean('conversion')->charset(null)->collation(null)->change();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('rooms', function (Blueprint $table) {
                $table->string('conversion')->change();
            });
        }
    

    我是根据查看framework/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php 的源代码得出这个答案的。您可以在此处看到,在您的实例中,您不希望为 bigint 指定字符集或排序规则。为了跳过这两个选项,我认为唯一的解决方案是手动将这两个值设置为 null。 Here is the source code MySQL 查询的那部分是在哪里形成的:

        /**
         * Append the character set specifications to a command.
         *
         * @param  string  $sql
         * @param  \Illuminate\Database\Connection  $connection
         * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
         * @return string
         */
        protected function compileCreateEncoding($sql, Connection $connection, Blueprint $blueprint)
        {
            // First we will set the character set if one has been set on either the create
            // blueprint itself or on the root configuration for the connection that the
            // table is being created on. We will add these to the create table query.
            if (isset($blueprint->charset)) {
                $sql .= ' default character set '.$blueprint->charset;
            } elseif (! is_null($charset = $connection->getConfig('charset'))) {
                $sql .= ' default character set '.$charset;
            }
    
            // Next we will add the collation to the create table statement if one has been
            // added to either this create table blueprint or the configuration for this
            // connection that the query is targeting. We'll add it to this SQL query.
            if (isset($blueprint->collation)) {
                $sql .= " collate '{$blueprint->collation}'";
            } elseif (! is_null($collation = $connection->getConfig('collation'))) {
                $sql .= " collate '{$collation}'";
            }
    
            return $sql;
        }
    

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 2017-10-29
      • 2015-10-12
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2014-01-14
      • 2013-12-02
      相关资源
      最近更新 更多