在Laravel中使用迁移重命名列。
在Laravel中使用迁移重命名列。
我使用的是laravel 5.6版本。
我想将author_ID
更改为user_id
。
我有以下所述的列:
class CreatePostsTable extends Migration{ public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->bigInteger('author_ID'); $table->text('title'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('posts'); } }
我使用以下链接来更改我的列名:
How can I rename a column in laravel using migration?
然后创建以下迁移:
php artisan make:migration rename_author_id_in_post_table
重命名迁移文件:
class UpdateUsernameInPostTable extends Migration{ public function up() { Schema::create('posts', function (Blueprint $table) { $table->renameColumn('author_ID', 'user_id'); }); } public function down() { Schema::create('posts', function (Blueprint $table) { $table->renameColumn('user_id', 'author_ID'); }); } }
但是使用运行命令:php artisan migrate
时,出现以下错误:
Symfony\Component\Debug\Exception\FatalThrowableError : Class 'RenameAuthorIdInPostTable' not found
问题:在Laravel中使用迁移重命名列的代码出现了问题。
原因:在原始迁移中使用了错误的方法Schema::create()
,而应该使用Schema::table()
方法来更新和改变字段。
解决方法:删除现有的迁移,然后使用以下代码创建一个新的迁移文件:
php artisan make:migration rename_author_id_in_posts_table --table=posts
该命令将创建一个新的迁移文件,并在该文件中替换up()和down()函数为以下代码:
public function up() { Schema::table('posts', function (Blueprint $table) { $table->renameColumn('author_ID', 'user_id'); }); }
以及以下代码:
public function down() { Schema::table('posts', function (Blueprint $table) { $table->renameColumn('user_id', 'author_ID'); }); }
通过执行php artisan migrate
命令来运行迁移。
更多关于迁移的信息,请参考Laravel Migrations。
问题的出现原因是类名与PHP迁移文件的名称不对应。文件名用于确定它们包含的类的名称,因此重命名类或文件时重命名另一个是很重要的。
将你的类名UpdateUsernameInPostTable
改为RenameAuthorIdInPostTable
,然后它应该可以工作。
如果没有,请运行composer dumpauto
重新加载自动加载文件,它肯定会工作。
感谢。我改变了类名并运行了composer dumpauto
。再次运行migrate
时,我遇到了以下错误:Symfony\Component\Debug\Exception\FatalThrowableError : Class Doctrine\DBAL\Driver\PDOMySql\Driver' not found
你需要添加以下包:doctrine/dbal
,运行命令composer require doctrine/dbal
,然后php artisan migrate
,它应该可以工作。
谢谢。我添加了doctrine/dbal
包,然后出现了新错误: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 server version for the right syntax to use near ') default character set utf8mb4 collate 'utf8mb4_unicode_ci'' at line 1 (SQL: create table 'posts' () default character set utf8mb4 collate 'utf8mb4_unicode_ci')
类名在这里没有问题。只需要使用Schema::table()
就可以解决这个错误。
是的,你说得对,但我非常确定类名仍然必须与文件名匹配,所以问题来自两者 🙂
在Laravel中使用迁移(migration)重命名列的问题是因为需要更改数据库表的列名而引起的。解决方法如下:
步骤1:运行以下命令创建一个新的迁移文件
php artisan make:migration update_author_ID_column --table=posts
步骤2:在创建的文件中写入以下代码,按照以下代码编写up()函数
public function up() { Schema::table('employee', function (Blueprint $table) { $table->renameColumn('author_ID', 'user_id'); }); }
步骤3:在down()函数中写入以下代码
public function down() { Schema::table('employee', function (Blueprint $table) { $table->renameColumn('user_id', 'author_ID'); }); }
步骤4:在执行迁移命令之前,安装doctrine/dbal依赖
composer require doctrine/dbal
步骤5:最后运行迁移命令
php artisan migrate