【问题标题】:rename column in laravel using migration使用迁移重命名laravel中的列
【发布时间】:2018-12-10 08:28:44
【问题描述】:

我使用 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 migration


    【解决方案1】:

    最好的方法是检查我们的表中是否有某些列,只是避免在迁移时出错;)

     public function up()
        {
            if (Schema::hasColumn('table_name', 'old_col_name'))
            {
                Schema::table('table_name', function (Blueprint $table)
                {
                    $table->renameColumn('old_col_name', 'new_col_name');
                });
            }
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            if (Schema::hasColumn('table_name', 'new_col_name'))
            {
                Schema::table('table_name', function (Blueprint $table)
                {
                    $table->renameColumn('new_col_name', 'old_col_name');
                });
            }
        }
    

    【讨论】:

      【解决方案2】:

      只需在删除现有迁移以更新名称后尝试此代码,

      php artisan make:migration rename_author_id_in_posts_table --table=posts
      

      它将创建一个迁移,然后在创建的迁移中用 this 替换 up() 和 down() 函数,

      public function up()
      {
          Schema::table('posts', function (Blueprint $table) {
              $table->renameColumn('author_ID', 'user_id');
          });
      }
      

      还有down()函数用这个,

      public function down()
      {
          Schema::table('posts', function (Blueprint $table) {
              $table->renameColumn('user_id', 'author_ID');
          });
      }
      

      有关迁移的更多信息,您可以通过此链接Laravel Migrations

      我希望这对你有用。如有任何疑问,请发表评论。

      您的迁移问题:

      您正在使用Schema::create()方法更改表,因为您已经为posts表创建了迁移,您不需要执行Schema::create(),只需使用Schema::table()方法更新和更改任何字段。

      Schema::create('posts', function (Blueprint $table) {
              $table->renameColumn('author_ID', 'user_id');
          });
      

      【讨论】:

      • 伟大的@MostafaNorzade
      【解决方案3】:

      错误来自您的类名与 PHP 迁移文件的名称不对应。文件名用于确定它们包含的类的名称,因此重要的是不要重命名一个类或一个文件而不重命名另一个。

      将你的班级 UpdateUsernameInPostTable 重命名为 RenameAuthorIdInPostTable,它应该可以工作。

      如果没有,运行composer dumpauto重新加载自动加载文件,它肯定会工作。

      【讨论】:

      • 谢谢。我更改了班级名称并运行 composer dumpauto 。运行migrate agin。我有打击错误: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() 即可解决错误。 @AntoineB
      • @ChiragPatel 是的,你确实是对的,但我相当确定类名仍然必须与文件名匹配,所以问题来自两者:)
      猜你喜欢
      • 2014-12-18
      • 2018-07-18
      • 2018-08-20
      • 2015-07-28
      • 2017-06-10
      • 2018-09-19
      • 2019-08-24
      • 2020-06-22
      • 1970-01-01
      相关资源
      最近更新 更多