8

I use laravel 5.6

I want to change author_ID to user_id

I have columns as mentioned below:

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');
    }
}

I use the below link to change my column name :

How can I rename a column in laravel using migration?

then create blow migration :

php artisan make:migration rename_author_id_in_post_table

rename migration file :

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');
        });
    }
}

but with the run command : php artisan migrate I have the below error :

Symfony\Component\Debug\Exception\FatalThrowableError  : 
Class 'RenameAuthorIdInPostTable' not found
Mostafa Norzade
  • 1,578
  • 5
  • 24
  • 40
  • 1
    For the duplicate, [this answer](https://stackoverflow.com/a/48603378/1255289) in particular. – miken32 Dec 06 '22 at 22:34

5 Answers5

25

Just try this code after deleting your existing migration for updating name,

php artisan make:migration rename_author_id_in_posts_table --table=posts

It will create one migration and then replace up() and down() functions with this in that created migration,

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

And down() function with this,

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

For more information about migration, you can go through this link Laravel Migrations.

PROBLEM WITH YOUR MIGRATION:

You are using Schema::create() method for change in table, As you have already created migration for posts table,You don't need to do Schema::create() just use Schema::table() method to update and change in any fields.

Schema::create('posts', function (Blueprint $table) {
        $table->renameColumn('author_ID', 'user_id');
    });
TylerH
  • 20,799
  • 66
  • 75
  • 101
Chirag Patel
  • 1,545
  • 2
  • 9
  • 17
5

The error comes from the fact that your class name doesn't correspond to the name of the PHP migration file. Filenames are used to determine the name of the class they contain, so it is important to not rename a class or a file without renaming the other.

Rename your class UpdateUsernameInPostTable to RenameAuthorIdInPostTable and it should work.

If it does not, run composer dumpauto to reload the autoloading file and it will definitely work.

AntoineB
  • 4,535
  • 5
  • 28
  • 61
  • Thanks . i changed my class name and run `composer dumpauto` . run `migrate` agin . i have blow error: `Symfony\Component\Debug\Exception\FatalThrowableError : Class Doctrine\DBAL\Driver\PDOMySql\Driver' not found` – Mostafa Norzade Jul 02 '18 at 07:04
  • You have to add the following package: `doctrine/dbal`, run the command `composer require doctrine/dbal`, and then `php artisan migrate`, it should work. – AntoineB Jul 02 '18 at 07:11
  • Thanks . i added `doctrine/dbal` package and i have new error : `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')` – Mostafa Norzade Jul 02 '18 at 07:23
  • Class names have not issue here. Just need to use `Schema::table()` will solve the error. @AntoineB – Chirag Patel Jul 02 '18 at 07:35
  • 1
    @ChiragPatel Yes indeed you are right, but I'm fairly certain that the class name still has to match the name of the file, so issue was coming from both :) – AntoineB Jul 02 '18 at 07:42
0

By following the below steps I can be able to rename the column name in Laravel 8
Step 1: Enter the below command to create a new migration file

php artisan make:migration update_author_ID_column --table=posts

Step 2: Write the below code in the created file. Follow the below code for up() function

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

Step 3: Write the Below Code in down() function

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

Step 4: Before running Migration command install the doctrine/dbal dependency

composer require doctrine/dbal

Step 5: finally run migration command.

php artisan migrate
-1

you can use DB statement if you don't want to use doctrine

public function up()
{
    DB::statement('ALTER TABLE posts CHANGE author_ID user_id bigint(20) ');
}
Rohallah Hatami
  • 525
  • 6
  • 12
-3

Best way would be to check if we have some column in our table or not, just avoid error while migrating;)

 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');
            });
        }
    }
  • This is a bad practice, you WANT your migration to fail if the column is missing, because it indicates that your database schema is wrong and not matching your migration files. Silently ignoring this can lead to your database being in an unknown state, which can lead to data corruption and downtime. – AntoineB Nov 03 '22 at 15:18
  • I dont agree to you, our validation will stop the whole flow from crashing at least, if you developing in right mind you will catch this at the run time – Ritesh Kumar Nov 03 '22 at 18:01
  • Yes, and catching something at runtime when you could have caught it when deploying or compiling is extremely bad. You absolutely want the flow to crash if your database is in a bad state. This is one of the main reasons for using migrations: consistency in your schema. – AntoineB Nov 05 '22 at 20:49