Ad

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?

Ad

Answer

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`= ?;
Ad
source: stackoverflow.com
Ad