CHANGE FILES ORDER

Jan 05, 2024 Copy Link

Let's pretend we want to create a Blog project on the fly so, we will create our migration files using the `make:migration` artisan command:

 

php artisan make:migration create_comments_table

 

The previous command will create a file in such timestamp_name_table format then, we will use the same command to create the `likes`, `images`, and `posts` migration files respectively. After that, we will add our columns in the `comments` migration file, and whatever columns you add, you definitely will add a `post_id` column which represents a foreign key to the `posts` table, and the same thing with the other tables

 

When you finish and intend to migrate your files into the database, an exception will be thrown, that is because you linked a `post_id` to the `posts` table that has not migrated yet that's because we have created the `comments` migration file before the `posts` migration file. However, you should not consider deleting all migration files and recreating them again in a proper order. By doing so you have committed a major crime because you certainly will not delete three migration files (the number may be larger) with their columns. But, Simply you can update the migration creation timestamp to be before or after the migration file that you want.

 

There is a nice way you can follow without the need to re-order the migrations, where you can create a special migration to add that foreign key, I used this way also in that Article.

Share via

Mahmoud Ramadan

Mahmoud Ramadan

Mahmoud is the creator of Digging Code and a contributor to Laravel since 2020.

Most recent

  • How to generate Arabic PDF using TCPDF

    How to generate Arabic PDF using...

    FREE

  • What is SQL Injection

    What is SQL Injection

    FREE

  • Leveraging virtual generated columns in MySQL using Laravel

    Leveraging virtual generated col...

    FREE