简体   繁体   English

每次迁移Laravel多个表

[英]Laravel multiple tables per migration

I am new to Laravel, so a bit new to this framework's best practices. 我是Laravel的新手,对这个框架的最佳实践有点新意。 I am trying to understand the best way to approach creating a database using migrations . 我试图了解使用迁移创建数据库的最佳方法。

The few examples I found on the web, including the Laravel documentation here and here , seem to refer to migration scripts that handle only one table. 我在网上找到的几个例子,包括这里这里的Laravel文档,似乎都是指只处理一个表的迁移脚本。 I am creating an application with around 10 tables, all interrelated with foreign keys between them, some with many-to-many relationships. 我正在创建一个包含大约10个表的应用程序,所有表都与它们之间的外键相互关联,其中一些表具有多对多关系。

  1. Is the recommended approach to have one migration file per table? 建议的方法是每个表有一个迁移文件吗? If so why? 如果是这样的话? (What are the disadvantages of putting all table creation scripts in one file, if any?) (将所有表创建脚本放在一个文件中有什么缺点,如果有的话?)

  2. What about foreign keys and relationships? 外键和关系怎么样? How does one enforce these relationships, and the order in which migrations are executed such that if table1 references a column in table2, table2 is created before table1? 如何强制执行这些关系,以及执行迁移的顺序,如果table1引用table2中的列,table2是否在table1之前创建?

  3. What about many-to-many relationships? 那么多对多关系怎么样? Does the relationship (pivot) table need to be created manually too through a separate migration script? 是否需要通过单独的迁移脚本手动创建关系(数据透视)表? If yes what ensures that it is created after the 2 related tables? 如果是,那么确保在2个相关表之后创建它?

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. 外键是一个很好的例子,说明为什么你应该为每个表甚至每个外键创建一个迁移:每次回滚一个与外键相关的表时,你必须先删除所有外来依赖项,这就是Laravel创建一个迁移它们的原因。相同的顺序,它可以帮助你永远不会拧下桌子。 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: 但是我总是在一个单独的Schema::table()命令中创建一个外键,因为在将约束附加到它之前,某些数据库需要你拥有该列:

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. 关于多对多,如果你创建表和外键togheter,你应该首先创建主表,然后创建数据透视表,但如果你在单独的迁移中创建外键,首先创建表(顺序无关紧要,但在这些情况下组织起来也更好)然后迁移外键。

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 1) php artisan migrate:reset多次

2) Alter migration 2)改变迁移

3) php artisan migrate 3) php artisan migrate

If I'm just creating a new one, usually I won't have any problems, because migrations are usually idepotent. 如果我只是创建一个新的,通常我不会有任何问题,因为迁移通常是idealotent。

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: 您的上一个问题已经得到解答,但我会再次说明:Laravel使用时间戳命名迁移文件,这种方式在您之前创建的另一个迁移之前永远不会进行迁移:

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. 所以你必须要做的一件事是用不同的名称创建每个迁移,否则你最终会得到两个具有相同名称的类,而不是Laravel,但PHP会抱怨它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM