Database: Migrations. Renaming Tables With Foreign Keys


Migrations are like variation control for the database, enabling your group to modify and share the application form’s database schema. Migrations are generally combined with Laravel’s schema builder to create the application’s database schema. Have you ever had to tell a teammate to manually include a line with their database that is local schema you have faced the issue that database migrations re re solve.

The Laravel Schema facade provides database support that is agnostic producing and manipulating tables across every one of Laravel’s supported database systems.

Generating Migrations

To produce a migration, make use of the make:migration Artisan command:

The migration that is new be positioned in your database/migrations directory. Each migration file title includes a timestamp, allowing Laravel to look for the purchase for the migrations.

The –table and –create choices could also be used to point the title associated with the dining dining table and set up migration are going to be developing a table that is new. These choices pre-fill the migration that is generated file using the specified dining dining table:

If you want to specify a customized production course when it comes to generated migration, you may possibly utilize the –path option whenever executing the make:migration demand. The given course must be relative to the application’s base course.

Squashing Migrations

While you create your application, you might accumulate increasingly more migrations with time. This could easily result in your migration directory becoming distended with possibly a huge selection of migrations. You may “squash” your migrations into a single SQL file if you would like. To begin with, perform the schema:dump demand:

Once you perform this demand, Laravel will compose a “schema” register to your database/schema directory. Now, once you try to migrate your database with no other migrations have now been performed, Laravel will perform the schema file’s SQL first. After performing the schema file’s commands, Laravel will perform any remaining migrations that are not the main schema dump.

You ought to commit your database schema file to source control to ensure other brand new designers on your group may quickly make your application’s initial database framework.

Migration squashing is designed for the MySQL, PostgreSQL, and SQLite databases. But, database dumps is almost certainly not restored to in-memory SQLite databases.

Migration Structure

A class that is migration two techniques: down and up . The up technique is used to incorporate brand new tables, columns, or indexes to your database, as the down method should reverse the operations done by the up technique.

Within these two practices you’ll make use of the Laravel schema builder to expressively produce and change tables. To know about most of the practices available from the Schema builder, always check its documentation out. As an example, the after migration produces a routes table:

Operating Migrations

To run your entire outstanding migrations, perform the migrate Artisan demand:

If you work with the Homestead digital device, you ought to run this demand from inside your digital device.

Forcing Migrations To Perform In Manufacturing

Some migration operations are destructive, this means they might lead you to lose information. So that you can protect you against operating these commands against your manufacturing database, you shall be prompted for verification ahead of the commands are performed. To force the commands to operate with no prompt, utilize the –force flag:

Rolling Right Straight Right Back Migrations

To move straight right back the migration operation that is latest, you might use the rollback demand. This command rolls right back the”batch that is last of migrations, which could add multiple migration files:

You could move right back a restricted wide range of migrations by giving the action solution to the rollback demand. For instance, the command that is following move straight straight straight back the past five migrations:

The migrate:reset command will move right back your entire application’s migrations:

Roll Straight Straight straight Back & Migrate Employing A solitary demand

The migrate:refresh demand will roll right straight back all your migrations then execute the command that is migrate. This demand efficiently re-creates your whole database:

You could move straight right back & re-migrate a restricted range migrations by giving the action choice to the refresh demand. For instance, the after command will move straight straight back & re-migrate the very last five migrations:

Drop All Tables & Migrate

The migrate:fresh demand will drop all tables through the database then perform the command that is migrate

The migrate:fresh demand will drop all database tables irrespective of their prefix. This demand should really be combined with care whenever developing on a database that is distributed to other applications.


Producing Tables

To generate a database that is new, make use of the make method in the Schema facade. The make technique takes two arguments: the foremost is the title regarding the dining table, even though the second is a closing which receives a Blueprint item that could be utilized to determine the table that is new

When designing the dining dining table, you might make use of some of the schema builder’s line ways to determine the dining dining table’s columns.

Checking For Table / Column Presence

You could look for the presence of a dining table or line making use of the hasTable and methods that are hasColumn

Database Connection & Table Alternatives

If you would like execute a schema procedure on a database connection that isn’t your standard connection, make use of the connection technique:

You might use the commands that are following the schema builder to define the dining dining dining table’s choices:

Renaming / Dropping Tables

To rename a database that is existing, make use of the rename technique:

To drop a table that is existing you may possibly make use of the fall or dropIfExists practices:

Before renaming a table, you need to validate that any key that is foreign up for grabs have an explicit title in your migration files as opposed to letting Laravel designate a meeting based title. Otherwise, the foreign key constraint name will relate to the old dining table title.


Creating Columns

The dining dining dining table technique in the Schema facade enables you to update tables that are existing. The table method accepts two arguments: the name of the table and a Closure that receives a Blueprint instance you may use to add columns to the table like the create method

Column Modifiers

In addition to the line types in the list above, there are numerous line “modifiers” you may utilize while incorporating a line to a database table. As an example, to really make the line “nullable”, you might use the nullable technique:

Default Expressions

A value is accepted by the default modifier or an \Illuminate\Database\Query\Expression example. Making use of a manifestation example will avoid wrapping the worth in title loans state of Arizona quotes and invite you to definitely utilize database functions that are specific. One situation where this will be especially helpful is whenever you will need to designate standard values to JSON columns:

Help for default expressions is dependent on your database driver, database variation, together with field kind. Please make reference to the documentation that is appropriate compatibility. Additionally remember that making use of database certain functions may tightly couple one to a driver that is specific.