亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel Migration SQLSTATE[42000]: 語法錯誤或訪問沖突: 1064

Laravel Migration SQLSTATE[42000]: 語法錯誤或訪問沖突: 1064

PHP
絕地無雙 2022-08-05 16:49:21
對于一個非常舊的遷移(過去運行正常),我收到一個新的遷移錯誤。我得到的錯誤是: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`)遷移文件如下所示:<?phpuse 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上運行。任何幫助將不勝感激。
查看完整描述

1 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

我相信Laravel如何配置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();

        });

    }

我基于查看 的源代碼來得出這個答案。您可以在此處看到,在您的實例中,您不希望為 bigint 指定字符集或排序規則。為了跳過這兩個選項,我認為唯一的解決方案是手動將這兩個值設置為 null。下面是形成MySQL查詢該部分的源代碼:framework/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php


    /**

     * 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;

    }


查看完整回答
反對 回復 2022-08-05
  • 1 回答
  • 0 關注
  • 99 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號