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

djarrin's avatar

Having trouble running php artisan migrate:refresh

When ever I try to run php artisan migrate:refresh I get one of two errors

if my tables haven't been removed manually I get: [Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'articles' already exists (SQL: create table articles (id int unsigned not null auto_increment primary key, user_id int unsigned not null, title varchar(255) not null, body text not nu
ll, created_at timestamp null, updated_at timestamp null, published_at timestamp not null) default character set utf8 collate utf8_unicode_ci)

and

[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'articles' already exists

I think this has something to do with my migrations down method

public function down()
    {
        Schema::drop('articles');
    }

Next if I go into my database and just manually delete the tables before running the refresh command I get this:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table articles add constraint articles_user_id_foreign foreign key (user_id) references `` (id) on delete cascade)

where my create method looks something like this

public function up()
    {
        Schema::create('articles', function(Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
            $table->timestamp('published_at');

            $table->foreign('user_id')
                    ->references("id")
                    ->onDelete('cascade');
        });

    }

any help/guidance is appreciated

0 likes
3 replies
jorgejavierleon's avatar
Level 16

Add the table to the on method $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')

djarrin's avatar

to the up method? I still seem to be getting the same error

djarrin's avatar

Ok after I manually deleted all of the my tables and ran php artisan migrate:refresh everything seemed function correctly. I noticed I had

 $table->foreign('user_id')
              ->references('id')
              ->onDelete('cascade');

before (missing the on('users')) so I guess I was missing some sort of relation to the users table there.

Please or to participate in this conversation.