php - Laravel 迁移 SQLSTATE[42000] : Syntax error or access violation: 1064

标签 php mysql laravel laravel-5.6

我在非常旧的迁移(曾经运行良好)中遇到了新的迁移错误。

我收到的错误是:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行 'CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`' 附近使用的正确语法(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`我收到错误:您有错误在你的 SQL 语法中;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行“CHARACTER SET utf8 NOT NULL”附近使用的正确语法

我正在 Homestead 上运行 Laravel 5.6。

如有任何帮助,我们将不胜感激。

最佳答案

我相信有一些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;
    }

关于php - Laravel 迁移 SQLSTATE[42000] : Syntax error or access violation: 1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59794644/

相关文章:

php - 验证node.js中的php证书

php - imagejpeg - 无法打开流

java - CLI 进程的线程池

MySQL 未在 Linux 机器上运行

php - Laravel 身份验证

PHP递归正则表达式过滤器迭代器迭代器

php - 如何从本地机器导出存储过程并导入到服务器?

php - 关联数组不存储 SQL 的第一个结果

php - 在 Laravel 6 中模拟 Auth::user()

php - 如何使用其他变量在路由中传递表单数据?