Fil
Fil

Reputation: 8873

Trying to rename a columns of a table in laravel throws error

I follow the guide here in modifying the column in Laravel.

I have table stores and run this in command line

php artisan make:migration rename_stores_column --table="stores" --create

After creating the migration

here the code

class RenameStoresColumn extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('stores', function (Blueprint $table) {
          $table->renameColumn('store_iamge', 'store_image');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('stores', function (Blueprint $table) {
          $table->renameColumn('store_image', 'store_iamge');
        });
    }
}

but when i run php artisan migrate

I got this error,

In Connection.php line 664:

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'stores' already exists (SQL: create table `stores` (`id` int unsigned not null auto_increment primary key, `name` varchar(255) null, `address` varchar(255) null, `city` varchar(255) null, `zipcode` varc
  har(255) null, `country` varchar(255) null, `state` varchar(255) null, `latitude` double(10, 6) not null, `longitude` double(10, 6) not null, `description` text null, `phone` varchar(255) null, `email` varchar(255) null, `fax` varchar(255) null, `web` varchar(255) n
  ull, `tags` varchar(255) null, `schedule` varchar(255) null, `store_iamge` varchar(255) null, `marker_image` varchar(255) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8 collate utf8_unicode_ci)


In PDOStatement.php line 143:

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'stores' already exists

what the proper way?

Upvotes: 4

Views: 390

Answers (2)

igs013
igs013

Reputation: 1420

It doesn't seem that your issue has anything to do with this rename_stores_column migration. It looks like php artisan migrate tries to rerun the create_stores_table migration even though it was already run.

Now you may come across this issue easily if at any point you have an error in one of your migrations, because the migration runs, it creates the table, but then the error occurs, the batch fails and it doesn't manage to insert the name of the migration in migrations table, so at the next migrate it will run it again, thus the issue you have here.

To solve this, you either run a fresh migrate (php artisan migrate:fresh) but only if you are on a dev environment and don't have any data to lose.

Or, in create_stores_table migration wrap the schema create in a schema has, so even if it tries to run it, if the table already exists it won't do anything:

if (!Schema::hasTable('stores')) {
  Schema::create('stores', function (Blueprint $table) {
    $table->increments('id');

    ...
  });
}

Upvotes: 1

Douwe de Haan
Douwe de Haan

Reputation: 6646

Did you require doctrine/dbal?

Prerequisites

Before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file. The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to the column:

composer require doctrine/dbal

Upvotes: 3

Related Questions