Search code examples
phplaravellaravel-5database-migrationlaravel-artisan

Is it possible to change the order in which tables migrate in Laravel?


I am trying to migrate my newly created migrations. The problem is that the batch of migrations waiting to migrate is in the order in which I created the migrations using

php artisan make:migration

And thus, my product_list migration is trying to set a foreign key on a table that it has not yet migrated.

$table->foreign('product_category')->references('id')->on('product_categories');
$table->foreign('product_type')->references('id')->on('product_types')->onDelete('cascade');

When I run php artisan migrate I get these errors:

C:\xampp\htdocs\iezonsolutions>php artisan migrate
Migrating: 2019_01_15_001617_product_list

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `iezonsolutions`.`#sql-1844_16e` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `product_list` add constraint `product_list_product_category_foreign` foreign key (`product_category`) references `product_categories` (`id`))

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

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `iezonsolutions`.`#sql-1844_16e` (errno: 150 "Foreign key constraint is incorrectly formed")")
      C:\xampp\htdocs\iezonsolutions\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\iezonsolutions\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  Please use the argument -v to see more details.

Is it possible to migrate my product_category and product_types tables before my product_list table? Then I will be able to set the foreign keys without errors.

My migration status looks like this:

+------+------------------------------------------------+-------+
| Ran? | Migration                                      | Batch |
+------+------------------------------------------------+-------+
| Yes  | 2014_10_12_000000_create_users_table           | 1     |
| Yes  | 2014_10_12_100000_create_password_resets_table | 1     |
| Yes  | 2019_01_14_203800_server_mode                  | 2     |
| No   | 2019_01_15_001617_product_list                 |       |
| No   | 2019_01_15_002318_product_types                |       |
| No   | 2019_01_15_002336_product_categories           |       |
| No   | 2019_01_15_002357_product_skus                 |       |
+------+------------------------------------------------+-------+

I want it to migrate in the order

| No   | 2019_01_15_002336_product_categories           |       |
| No   | 2019_01_15_002318_product_types                |       |
| No   | 2019_01_15_001617_product_list                 |       |
| No   | 2019_01_15_002357_product_skus                 |       |
+------+------------------------------------------------+-------+

Solution

  • The simplest thing you can do is order the date and time on filename which eventually followed by laravel to migrate database sequentially.