Яндекс.Метрика
qr-код - Как-то так стрелка - Как-то так
Leading Economist + ... пиктограмма - Как-то Так THIS     Economic literacy and web solutions
All on the topic: what, how and why. Actual things - in their own words.
Turn your quality WITH ME - BUTW !
If you don't like what you get, change what you give. © Carlos Castaneda     KAKTOTAK.BY - economic design and web development services (php/js - telegram, WordPress, Laravel, Vue, SEO)    Cooperation - on an ongoing basis    Be in the topic   -    SUBSCRIBE      My notes are ECONOMICS +  и  WEB Developer's Guide | php+js+seo   в Telegram   telegram  

Error during migration to Laravel: the order of migrations when working with foreign keys and tables

Error during migration to Laravel: the order of migrations when working with foreign keys and tables

Sometimes situations arise in Laravel when creating separate database tables, when the order migrations affects the successful execution of their deployment operations.

For example, the situation

You are doing a project on Laravel, and it became necessary to add a new table to the database (unit_orders - as an example), which will be linked by its primary key to the table (employers - as an example) previously created.

Before adding a new table (by migration), we roll back all existing migrations to their original state with the command php artisan migrate:rollback.

Next, we create a new migration for our new table using the php artisan make:model UnitOrder -mcr command.

As a result, the "bouquet" we need is created - the controller, the model and the migration. But the created migration ends up at the bottom.

In the migrations folder, the migration files will be arranged in chronological order from the dates (timestamps) of their creation.

the order of migrations when working with external keys is 4

If we roll out all the migrations for our project now, we will get the following type of error:

"An already created table cannot add the foreign key of a table that does not yet exist."

order of migrations when working with foreign keys - 1

Thus, in order to perform all migrations correctly (creating all project tables), it is necessary to transfer the last migration to create our new unit_orders table before the previously created employers table. This is done by changing the date of its creation to an arbitrary date, which must precede the date of creation of the existing employers table.

Before performing migrations, completely clean the database (this can even be done manually via the graphical database client if any errors occur on the part of the php artisan migrate:rollback team).

order of migrations when working with foreign keys -2

After that, we run the standard command php artisan migrate, and all migrations are successful.

order of migrations when working with foreign keys -3

автор - Михаленко Р.
M R. Автор - kaktotak.by Specialization: financial and economic design - modeling of business, investment projects of the real sector, analysis and evaluation of efficiency, optimization of the management decision system.

A wide range of web-based competencies for solving business problems.

Subscribe to my telegram channel - My notes are ECONOMICS +
Там я ничего не втюхиваю и не навязываю. Просто делюсь полезной информацией по экономической грамотности. Повышайте своё качество вместе со мной: что, как и почему в экономике на простом языке. Понятия, алгоритмы, процессы, микро- и макроаспекты и многое другое. Может, будет интересно !

And also - WEB Developer's Guide | php+js+seo
Notes and native solutions to simple local tasks in PHP, JS. Something about Laravel, WordPress, Vue and SEO.

  Personal assistance in economic design and web development:

  • Financial and economic modeling, analysis, accounting, business planning
  • Comprehensive web development/project support on the web (php/js, seo – Laravel, WordPress, Vue, telegram, administration, content, advertising in Yandex Direct

  telegram или форма обратной связи

Administrator
28
0
Name
E-mail
Rating
Review

Currency Converter
RUB RUB-icon
USD USD-icon
EUR EUR-icon
CNY CNY-icon
BYN BYN-icon
UAH UAH-icon
KZT KZT-icon
SHORT- what is it about
ECONOMIC LITERACY
  Simple online solutions to problems of economics and finance  
I Want To Know Everything. Useful Tips