在迁移中运行原始SQL [英] Run raw SQL in migration
问题描述
我正在尝试使用任何语法,但无法思考如何正确编写此代码:
I was trying with whatever syntax and can't think how can I write this correctly:
Schema::table('users', function(Blueprint $table){
$sql = <<<SQL
ALTER TABLE 'users' MODIFY 'age' DATETIME
SQL;
DB::connection()->getPdo()->exec($sql);
});
也尝试过
DB::statement('ALTER TABLE \'users\' MODIFY COLUMN DATETIME);
和双引号等.运行迁移时,总是会得到以下信息:
and double quotation marks and so on. I always get the following when I run the migration:
语法错误或访问冲突:1064您的SQL语法有错误;检查
与您的MariaDB服务器版本相对应的手册,以在第1行的用户"修改年龄" DATETIME"附近使用正确的语法
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 ''users' MODIFY 'age' DATETIME' at line 1
是的,我已经检查过,MariaDB使用MySQL的语法(至少在这种情况下如此).
Yes, I have checked, MariaDB uses MySQL's syntax (at least for this case).
推荐答案
在MySQL中使用反引号而不是单引号来转义标识符:
Use back-ticks instead of single quotes to escape identifiers in MySQL:
alter table `users` modify `age` datetime
在这种情况下,您可以完全忽略转义:
alter table users modify age datetime
这篇关于在迁移中运行原始SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!