Laravel multiple tables per migration

后端 未结 1 884
灰色年华
灰色年华 2021-02-01 04:50

I am new to Laravel, so a bit new to this framework\'s best practices. I am trying to understand the best way to approach creating a database using migrations.

The few e

相关标签:
1条回答
  • 2021-02-01 05:02

    During development of your application I don't think you should care too much having only one table per migration, sometimes it's just easier to have some tables togheter in a single migration, but as soon as your system go to production, you will not be able to keep working like that, because you will only migrate in production and probably never rollback, so your migrations will be really small, sometimes you'll have a migration for a single column creation.

    The advantages of putting tables in different migrations is the same of having a thin class, the less information you have in one file, the easier is to manage and make changes on it. So if you put all tables in a single migration, it gets harder to maintain, but that's really up to you.

    Foreign keys are a good example of why you should create one migration per table and even per foreign key: every time you rollback a table related to foreign keys, you must first delete all foreign dependencies, that's why Laravel creates a migrates them all in the same order, it helps you never screw a table drop. So, create your tables migrations first, then you create your foreign keys migrations, so when you rollback it will first rollback the constraints and then the tables.

    I create foreign keys for a table in the same migration of that table, unless I have too much cross foreign keys. But I always create a foreign key in a separate Schema::table() command, because some databases need you to have the column before attaching the constraint to it:

    public function up()
    {
        Schema::create('statuses', function(Blueprint $table)
        {
            $table->string('id', 64)->primary();
    
            $table->string('user_id', 64)->index();
            $table->text('body');
    
            $table->timestamps();
        });
    
        Schema::table('statuses', function(Blueprint $table)
        {
            $table->foreign('user_id')
                    ->references('id')
                    ->on('users')
                    ->onUpdate('cascade')
                    ->onDelete('cascade');
        });
    }
    

    About many to many, if you create the table and foreign keys togheter, you should first create the master and then the pivot tables, but if you are creating your foreign keys in separate migrations, first create the tables (order will not matter much, but it's also better to be organized in those cases) and then the migrations for the foreign keys.

    During development I do a lot of changes in my tables, so I'm always coming back to them, so this is what I use to do, when I'm altering a migration:

    1) php artisan migrate:reset many times

    2) Alter migration

    3) php artisan migrate

    If I'm just creating a new one, usually I won't have any problems, because migrations are usually idepotent.

    Your last question was already answered, but I'll say it again: Laravel name the migrations files using timestamps, the way you will never have a migration being ran before another one created before it:

    2014_07_16_190821_create_statuses_table
    

    And the name of the migration matter, because this one above will create this class:

    CreateStatusesTable
    

    So one thing you must do is to create every migration with a different name, otherwise you will end up with two classes with the same name and, not Laravel, but PHP will complaint about it.

    0 讨论(0)
提交回复
热议问题