Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

OzzDev's avatar

How to run migrations even if the table already exist?

I ran php artisan migrate:fresh to delete every table, then I have a sql dump file that also creates the tables and populates them with some data. However then I also need to run php artisan migrate since there are some migrations that are needed to add some extra columns to some tables (and these columns are not in the sql dump file I'm using).

So, I run php artisan migrate I'm always getting some errors like this:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'categories' already exists (SQL: create table `categories` (`id` bigint unsigned not null auto_increment primary key, `title` varchar(255) not null, `description` varchar(255) not null, `is_active` tinyint(1) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

In fact the table already exist but there are some other migrations that are necessary to run besides this one to create the categories table. There is some way to ignore this error or something like that so the other migrations that didnt the job yet can be executed?

0 likes
6 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Just surround your migration with a check:

if ( ! Schema::hasTable('categories'))
{
	Schema::create('categories', function (Blueprint $table) {
...
	});
}
1 like
OzzDev's avatar

@Nakov Thanks, so basically it's a good approach to do this for all the migrations? Because for example if I do this for the categories table then I'm getting the same error for the _create_password_resets_table, and so on.

Nakov's avatar

@OzzDev it won't hurt, but it really to me personally does not make sense to have to add it to all the migrations. That means that you had to change some ordering, and if you change order then the later migrations depend on that one from before in which case you will have to migrate fresh instead of adding the check. But you know your code better. I just know that this does not hurt if you have to do it, and it is basically the only way to check.

Snapey's avatar

does your dump file not contain the migrations table?

It should be possible to restore the dump, then run migrations so that any new migrations since the dump was taken are ran

Please or to participate in this conversation.