phplaravellaravel-5.8laravel-migrationsartisan-migrate

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?


Solution

  • 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,

    UPDATE `migrations` SET `migration`='2016_08_31_104213_create_financial_years_table' WHERE `id`= ?;