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

Davieboy's avatar

php artisan migrate

Had a bit of a faux pas and duplicated a migration, renamed it and setup tables but forgot to rename the table , i noticed when i ran migrate and it failed because another table with the name already exists, I changed it to the correct name it should be and ran it again and everything was fine, carried on with forum tutorials.

But now anytime i try to run a migration i get a error relating to that other migration

www@64445eab230f:/var/www$ php artisan migrate:refresh
Rolling back: 2020_03_27_151754_create_activities_table
Rolled back:  2020_03_27_151754_create_activities_table (0.04 seconds)
Rolling back: 2020_03_26_143240_create_favorites_table
Rolled back:  2020_03_26_143240_create_favorites_table (0.05 seconds)
Rolling back: 2020_02_02_222508_create_businesses_contacts_table
Rolled back:  2020_02_02_222508_create_businesses_contacts_table (0.03 seconds)
Rolling back: 2020_02_02_154503_create_contacts_table
Rolled back:  2020_02_02_154503_create_contacts_table (0.06 seconds)
Rolling back: 2020_01_30_200801_create_businesses_table
Rolled back:  2020_01_30_200801_create_businesses_table (0.04 seconds)
Rolling back: 2020_01_24_163656_create_customers_table
Rolled back:  2020_01_24_163656_create_customers_table (0.04 seconds)
Rolling back: 2020_01_02_132824_create_channels_table
Rolled back:  2020_01_02_132824_create_channels_table (0.03 seconds)
Rolling back: 2019_12_05_014216_create_replies_table
Rolled back:  2019_12_05_014216_create_replies_table (0.03 seconds)
Rolling back: 2019_12_05_013741_create_threads_table
Rolled back:  2019_12_05_013741_create_threads_table (0.04 seconds)
Rolling back: 2019_08_19_000000_create_failed_jobs_table
Rolled back:  2019_08_19_000000_create_failed_jobs_table (0.02 seconds)
Rolling back: 2018_08_08_100000_create_telescope_entries_table
Rolled back:  2018_08_08_100000_create_telescope_entries_table (0.12 seconds)
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back:  2014_10_12_100000_create_password_resets_table (0.04 seconds)
Rolling back: 2014_10_12_000000_create_users_table
Rolled back:  2014_10_12_000000_create_users_table (0.04 seconds)
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.11 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.11 seconds)
Migrating: 2018_08_08_100000_create_telescope_entries_table
Migrated:  2018_08_08_100000_create_telescope_entries_table (0.66 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.08 seconds)
Migrating: 2019_12_05_013741_create_threads_table
Migrated:  2019_12_05_013741_create_threads_table (0.07 seconds)
Migrating: 2019_12_05_014216_create_replies_table
Migrated:  2019_12_05_014216_create_replies_table (0.07 seconds)
Migrating: 2020_01_02_132824_create_channels_table
Migrated:  2020_01_02_132824_create_channels_table (0.06 seconds)
Migrating: 2020_01_24_163656_create_customers_table
Migrated:  2020_01_24_163656_create_customers_table (0.07 seconds)
Migrating: 2020_01_30_200801_create_businesses_table
Migrated:  2020_01_30_200801_create_businesses_table (0.37 seconds)
Migrating: 2020_02_02_154503_create_contacts_table
Migrated:  2020_02_02_154503_create_contacts_table (0.06 seconds)
Migrating: 2020_02_02_222508_create_businesses_contacts_table
Migrated:  2020_02_02_222508_create_businesses_contacts_table (0.66 seconds)
Migrating: 2020_03_26_143240_create_favorites_table
Migrated:  2020_03_26_143240_create_favorites_table (0.11 seconds)
Migrating: 2020_03_27_151754_create_activities_table
Migrated:  2020_03_27_151754_create_activities_table (0.18 seconds)
Migrating: 2020_04_02_182917_create_thread_subscriptions_table

   Illuminate\Database\QueryException  : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'thread_subscriptions' already exists (SQL: create table `thread_subscriptions` (`id` int unsigned not null auto_increment primary key, `user_id` int unsigned not null, `thread_id` int unsigned not null, `cre                                                                                 ated_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')                                                                                                                            

  at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

  Exception trace:

  1   PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'thread_subscriptions' already exists")
      /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463

  2   PDOStatement::execute()
      /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463

  Please use the argument -v to see more details.

I have tried dropping all tables and running it again but it still fails with the folling error


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

  at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
      /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463

  2   PDOStatement::execute()
      /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463

  Please use the argument -v to see more details.

if i run migrate again after that it goes back to the previous error, I have double checked my code and it is exactly the same as the the github source https://github.com/laracasts/Lets-Build-a-Forum-in-Laravel/blob/master/database/migrations/2017_05_19_141013_create_thread_subscriptions_table.php

any ideas?

0 likes
5 replies
Snapey's avatar

if you run migrate:fresh (assuming you have no data you need to keep !! ) then the tables are all dropped rather than rolling back.

This includes the migrations table, which it sounds like is messed up.

Davieboy's avatar

Snapey: goes back to this error

www@e51f621c0e8e:/var/www$ php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.13 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.11 seconds)
Migrating: 2018_08_08_100000_create_telescope_entries_table
Migrated:  2018_08_08_100000_create_telescope_entries_table (0.81 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.08 seconds)
Migrating: 2019_12_05_013741_create_threads_table
Migrated:  2019_12_05_013741_create_threads_table (0.1 seconds)
Migrating: 2019_12_05_014216_create_replies_table
Migrated:  2019_12_05_014216_create_replies_table (0.08 seconds)
Migrating: 2020_01_02_132824_create_channels_table
Migrated:  2020_01_02_132824_create_channels_table (0.06 seconds)
Migrating: 2020_01_24_163656_create_customers_table
Migrated:  2020_01_24_163656_create_customers_table (0.06 seconds)
Migrating: 2020_01_30_200801_create_businesses_table
Migrated:  2020_01_30_200801_create_businesses_table (0.12 seconds)
Migrating: 2020_02_02_154503_create_contacts_table
Migrated:  2020_02_02_154503_create_contacts_table (0.06 seconds)
Migrating: 2020_02_02_222508_create_businesses_contacts_table
Migrated:  2020_02_02_222508_create_businesses_contacts_table (0.51 seconds)
Migrating: 2020_03_26_143240_create_favorites_table
Migrated:  2020_03_26_143240_create_favorites_table (0.16 seconds)
Migrating: 2020_03_27_151754_create_activities_table
Migrated:  2020_03_27_151754_create_activities_table (0.2 seconds)
Migrating: 2020_04_02_182917_create_thread_subscriptions_table

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

  at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error                                                                                                                                            
    666|         // message to include the bindings with SQL, which will make this exception a                                                                                                                                               
    667|         // lot more helpful to the developer instead of just the database's errors.                                                                                                                                                 
    668|         catch (Exception $e) {                                                                                                                                                                                                      
  > 669|             throw new QueryException(                                                                                                                                                                                               
    670|                 $query, $this->prepareBindings($bindings), $e                                                                                                                                                                       
    671|             );                                                                                                                                                                                                                      
    672|         }                                                                                                                                                                                                                           
    673|                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                             
  Exception trace:                                                                                                                                                                                                                           
                                                                                                                                                                                                                                             
  1   PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")                                                                                                                                               
      /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463                                                                                                                                                           
                                                                                                                                                                                                                                             
  2   PDOStatement::execute()
      /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:463

  Please use the argument -v to see more details.
Snapey's avatar

ok, so there is an issue with your foreign key constraint. examine the migration and fix the problem.

Make sure that the column types are identical.

Davieboy's avatar

veggirl123 found the solution in the comments to the video, all sorted again.

$table->unsignedBigInteger('thread_id');

Please or to participate in this conversation.