我使用Laravel迁移创建了一个表。我迁移了两个字段作为字符串,但我希望一个字段作为日期,一个字段作为整数。因此,我创建了一个新的迁移来更改这些字段。我安装了理论/dbal。我用的是拉拉维尔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();
});
}但我犯了个错误。
说明\数据库\QueryException: SQLSTATE42000:语法错误或访问冲突: 1064您的MySQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以获得在第1行使用接近“字符集utf8mb4默认空排序规则utf8mb4_unicode_ci,更改递归”的正确语法( SQL : ALTER recurring_tasks CHANGE next_recurring_date next_recurring_date DATE SET utf8mb4 DEFAULT NULL COLLATE 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::("SQLSTATE42000:语法错误或访问冲突: 1064您的SQL语法出现错误;检查与MySQL服务器版本对应的手册,以获得使用接近“字符集utf8mb4默认空排序规则utf8mb4_unicode_ci,在第1行更改递归”的正确语法)
2 /home/vagrant/laravel-api/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:61::(“SQLSTATE42000:语法错误或访问冲突:SQLSTATE42000语法中有错误;检查与MySQL服务器版本对应的手册,以获得使用接近‘字符集utf8mb4默认空排序规则utf8mb4_unicode_ci,在第1行更改递归’的正确语法”)
请使用参数-v查看更多细节。
发布于 2019-12-18 10:46:58
这可能是因为doctrine/dbal v2.10.0包的最新版本存在缺陷。
您可以将doctrine/dbal包在composer.json中降级为v2.9.3。
https://github.com/laravel/framework/issues/30539#issuecomment-559605145
您可以尝试在迁移up()时像这样使用原始sql查询。
DB::statement("ALTER TABLE .....");发布于 2019-12-18 12:59:27
我们也可以用
$table->date('next_follow_date')->charset('')->collation('')->change();为了避免这个问题
https://stackoverflow.com/questions/59390059
复制相似问题