简体   繁体   中英

Laravel Migration: Base table or view already exists

Background

We're using Laravel (5.8) migration for one of our projects from the very beginning. During development we made some migrations. After some time, we found that, some of the migrations are related to setup/configuration. So we moved them up by renaming the migration files, like, from:

2019_08_05_104213_create_financial_years_table

to

2016_08_31_104213_create_financial_years_table

After then, we moved forward, and in some stage we made more migration files and then ran php artisan migrate . But it came up with error:

Base table or view already exists: ... Table 'financial_years' already exists

So, we tried deleting the base table (in this case: financial_years ), and then deleted the row mentioning '...financial_years...' from the migrations table too.

But the php artisan migrate came up with the same error again and again. We inspected the whole database, but found no index or table of financial_years .

Known remedy, but

We know that, we can run php artisan migrate:refresh for a fresh migration. But the data we have in our database is important, we don't want to mess with the data right now. We might go for a fresh migration when to proceed to the production, but not now.

How can we proceed with the Laravel migration in this scenario?

Your migrations table has an old entry by name of your old file 2019_08_05_104213_create_financial_years_table . Now since you have changed the file name and Laravel thinks this is a new migration. So it runs that file too.

The quick fix is to edit the filename in migrations table also.

The exception throws when you run migration command php artisan migrate because migration actually wants the parent tables first, and then the child tables when there are relationships in between them.

Solution

In your case,

  • in /migrations/ directory rename your migration files so that the parent migration table timestamp appears before the child table's migration, and
  • update the migrations table using the following command (before running that, find respective the row id and place it in ? )
UPDATE `migrations` SET `migration`='2016_08_31_104213_create_financial_years_table' WHERE `id`= ?;

Though we don't know the cause of the issue yet, we, actually stuck in the middle of nowhere. In this barren land, one of our colleagues (Mr. Shakhawat Hossain) came up with a different solution:

  • We restored the database table financial_years , and restored the migrations table to its previous state. (means we rolled back to the default state 😀)
  • We then Cut ( Ctrl + x or + x ) the migration files, that are causing issues, from the database/migrations/ to somewhere else
  • Then ran php artisan migrate . It successfully ran all the new migrations without any error.
  • We then bring back the moved migration files where they were previously

Problem is solved for now.

But the ultimate solution would be php artisan migrate:refresh - we're informed. But you know, it's not our case just now.

Please check first in migration table in database that migration doesn'ton same name, if exists already than delete it and run again. You can try to add in migration:

Schema::drop('financial_years');

Laravel assumes that you are trying to run a new migration. When it runs it finds that entry in the migration table exists since you only changed the name. 'php artisan migrate:reset' would be the best solution but since you dont want to loose your data, you will have to fix the filename in the migration table manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM