首页 > 解决方案 > Doctrine/dbal - 尝试将字符串字段更改为日期时出错

问题描述

我使用 Laravel 迁移创建了一个表。我将两个字段作为字符串迁移,但我想要一个作为日期,一个作为整数。所以我创建了一个新的迁移来更改这些字段。我安装了学说/dbal。我使用 Laravel 6.5。但是,尝试迁移时出现错误。

移民

public function up()
{
    Schema::table('follow_up_task', function (Blueprint $table) {
        $table->date('next_follow_date')->change();
        $table->integer('follow_stop_after')->change();
    });
}

public function down()
{
    Schema::table('follow_up_task', function (Blueprint $table) {
        $table->string('next_follow_date')->change();
        $table->string('follow_stop_after')->change();
    });
}

但我得到了一个错误。

Illuminate\Database\QueryException : SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以utf8mb4_unicode_ci在第 1 行的 'CHARACTER SET utf8mb4 DEFAULT NULL COLLATE , CHANGE recurrin' 附近使用正确的语法(SQL:ALTER TABLE recurring_tasks CHANGE next_recurring_date next_recurring_date DATE CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci, CHANGE recurring_stop_after recurring_stop_after INT CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci)

at /home/vagrant/laravel-api/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
665|         // If an exception occurs when attempting to run a query, we'll format the error
666|         // message to include the bindings with SQL, which will make this exception a
667|         // lot more helpful to the developer instead of just the database's errors.
668|         catch (Exception $e) {
669|             throw new QueryException(
670|                 $query, $this->prepareBindings($bindings), $e
671|             );
672|         }
673| 

Exception trace:

1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册以获取正确的语法以在附近使用'CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci, CHANGE recurrin' at line 1") /home/vagrant/laravel-api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:63

2 PDOException::("SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在 'CHARACTER SET utf8mb4 DEFAULT NULL 附近使用正确的语法COLLATE utf8mb4_unicode_ci, CHANGE recurrin' at line 1") /home/vagrant/laravel-api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:61

请使用参数 -v 查看更多详细信息。

标签: laraveldoctrinedbal

解决方案


我们也可以使用

$table->date('next_follow_date')->charset('')->collation('')->change();

为了避免这个问题


推荐阅读