0

I've successfully renamed an existing column previously created in a different migration with the following line of code:

$table->renameColumn('custom_paper_note', 'custom_primary_paper_note');

However, now when I run php artisan migrate:refresh I get the following errors:

[Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'custom_paper_note'; check that column/key exists (SQL: alter table `line_items` drop `custom_paper_note`)

and

[PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'custom_paper_note'; check that column/key exists

which all makes good sense to me because I renamed the column and now it can't drop it during the migrate:refresh process. However, I don't understand how to go about fixing this error?

Thanks for your help.

Bryan Miller
  • 3,262
  • 4
  • 27
  • 51

2 Answers2

1

Looks like you made the rename operation already and it can't find the old name.

Adding an if condition which checks column existence before executing this migration line could be useful.

mirza
  • 5,685
  • 10
  • 43
  • 73
  • Thanks for the response. Unfortunately, I cannot make use of the dropIfExists() method because I would have to change a previous migration file in this situation. It was a good idea though if I were able to alter the previous migration. – Bryan Miller Jul 09 '16 at 00:38
1

In the same migration file, in the down() function, declare the inverse of the rename:

Schema::table('table', function($table){
    $table->renameColumn('custom_primary_paper_note', 'custom_paper_note');
});

This way when you bring it back down, it will rename the column to the appropriate column name so that it's backwards compatible.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110