Doctrine/dbal - 尝试将字符串字段更改为日期时出错
Doctrine/dbal - Got error when try to change a string field to date
我使用 Laravel 迁移创建了 table。我将两个字段作为字符串迁移,但我想要一个作为日期,一个作为整数。所以我创建了一个新的迁移来更改这些字段。我安装了 doctrine/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 服务器版本相对应的手册,了解在第 1 行 'CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci
, CHANGE recurrin' 附近使用的正确语法(SQL:ALTER TABLE recurring_tasks CHANGE next_recurring_date next_recurring_date 日期字符集 utf8mb4 默认空整理 utf8mb4_unicode_ci
,更改 recurring_stop_after recurring_stop_after INT 字符集 utf8mb4 默认空整理 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]: 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 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]: 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 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查看更多详情。
那可能是因为 doctrine/dbal
v2.10.0
软件包的最新版本中存在错误。
您可以将 composer.json
中的 doctrine/dbal
软件包降级到 v2.9.3
。
https://github.com/laravel/framework/issues/30539#issuecomment-559605145
您可以像这样尝试在迁移 up()
中使用原始 sql 查询。
DB::statement("ALTER TABLE .....");
我们也可以使用
$table->date('next_follow_date')->charset('')->collation('')->change();
为了避免这个问题
我使用 Laravel 迁移创建了 table。我将两个字段作为字符串迁移,但我想要一个作为日期,一个作为整数。所以我创建了一个新的迁移来更改这些字段。我安装了 doctrine/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 服务器版本相对应的手册,了解在第 1 行 'CHARACTER SET utf8mb4 DEFAULT NULL COLLATE utf8mb4_unicode_ci
, CHANGE recurrin' 附近使用的正确语法(SQL:ALTER TABLE recurring_tasks CHANGE next_recurring_date next_recurring_date 日期字符集 utf8mb4 默认空整理 utf8mb4_unicode_ci
,更改 recurring_stop_after recurring_stop_after INT 字符集 utf8mb4 默认空整理 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]: 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 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]: 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 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查看更多详情。
那可能是因为 doctrine/dbal
v2.10.0
软件包的最新版本中存在错误。
您可以将 composer.json
中的 doctrine/dbal
软件包降级到 v2.9.3
。
https://github.com/laravel/framework/issues/30539#issuecomment-559605145
您可以像这样尝试在迁移 up()
中使用原始 sql 查询。
DB::statement("ALTER TABLE .....");
我们也可以使用
$table->date('next_follow_date')->charset('')->collation('')->change();
为了避免这个问题