【问题标题】:Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;
【发布时间】:2020-03-02 10:23:49
【问题描述】:

我想使用 laravel 迁移我的数据库。
我试试这个命令:php artisan migrate:fresh --seed 并接受这个错误:

Migrating: 2019_10_28_130723_alter_users_table

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB ser
ver version for the right syntax to use near 'CHARACTER SET utf8mb4 DEFAULT 0 NOT NULL COLLATE `utf8mb4_unicode_ci`' at line 1 (SQL: ALTER TABLE users CHANGE deductible_amount deductible_a
mount BIGINT UNSIGNED CHARACTER SET utf8mb4 DEFAULT 0 NOT NULL COLLATE `utf8mb4_unicode_ci`)

  at C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB s
erver version for the right syntax to use near 'CHARACTER SET utf8mb4 DEFAULT 0 NOT NULL COLLATE `utf8mb4_unicode_ci`' at line 1")
      C:\xampp\htdocs\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:63

  2   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the
 right syntax to use near 'CHARACTER SET utf8mb4 DEFAULT 0 NOT NULL COLLATE `utf8mb4_unicode_ci`' at line 1")
      C:\xampp\htdocs\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:61

  Please use the argument -v to see more details.

这是我的2019_10_28_130723_alter_users_table迁移文件的内容:

<?php

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

class AlterUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->renameColumn('active', 'is_active');
            $table->dropColumn([
                                    'settings',
                                    'google_id',
                                    'github_id',
                                    'telegram_notif',
                                    'income',
                                    'email_notif',
                                    'sms_notif',
                                    'temppass',
                                    'storm_id',
                               ]);
            $table->unsignedBigInteger('deductible_amount')->change();
            $table->softDeletes();
        });
    }



    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->renameColumn('is_active', 'active');
            $table->text('settings');
            $table->string('google_id')->nullable();
            $table->string('github_id')->nullable();
            $table->boolean('telegram_notif')->default(true);
            $table->string('income')->nullable();
            $table->boolean('email_notif')->default(true);
            $table->boolean('sms_notif')->default(false);
            $table->string('temppass')->nullable();
            $table->string('storm_id')->default(null)->nullable();
            $table->string('deductible_amount')->after('wallet')->default('0')->change();
            $table->dropSoftDeletes();
        });
    }
}

我不知道如何解决这个问题。我已经搜索过了,但大多数答案都提到了一个必须转换为stringjson 字段,而我的users 表上没有json 字段。 问题出在哪里,我该如何解决?

【问题讨论】:

    标签: php mysql database laravel mariadb


    【解决方案1】:

    此行会导致您的迁移出错。

    $table->unsignedBigInteger('deductible_amount')->change();
    

    试着改成

    $table->string('deductible_amount')->unsigned()->change();
    

    根据您的情况更新答案。

            Schema::table('users', function (Blueprint $table) {
                $table->unsignedBigInteger('deductible_amount_new');
            });
    
            DB::statement('UPDATE `users` SET deductible_amount_new=deductible_amount ');
    
            Schema::table('users', function (Blueprint $table) {
                $table->renameColumn('active', 'is_active');
                $table->dropColumn([
                                        'settings',
                                        'google_id',
                                        'github_id',
                                        'telegram_notif',
                                        'income',
                                        'email_notif',
                                        'sms_notif',
                                        'temppass',
                                        'storm_id',
                                   ]);
                $table->dropColumn('deductible_amount');
                $table->softDeletes();
            });
    
            Schema::table('users', function (Blueprint $table) {
                $table->renameColumn('deductible_amount_new', 'deductible_amount');
            });
    

    希望对你有帮助。

    【讨论】:

    • 该行在 ˋdown` 方法上,将在回滚命令上触发。我知道问题出在 ˋdeductible_amount` 字段上,但在up 方法上,因为我正在尝试php artisan migrate commanr。
    • 你在说什么?该行在代码中的up 方法上,尝试将类型更改为string,问题将得到解决。稍后谢谢我。
    • 对不起,这是从另一个错误答案中复制的评论,现在已被该用户删除。但我需要这个字段是整数,因为它存储的是整数,而不是字符串。
    • 这个答案甚至没有解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2020-05-03
    • 2020-02-15
    • 2013-06-27
    • 2022-01-11
    相关资源
    最近更新 更多