Laravel更改迁移顺序

Jam*_*mie 19 migration laravel-5

有没有办法在不重新制作迁移顺序的情况下更改迁移顺序?因为现在我的外键有问题-_-(使用laravel)

Kry*_*ten 52

  1. 回滚所有迁移(或从新数据库开始);

  2. 更改构成迁移文件名第一部分的日期,使其符合您的要求(例如2014_06_24_134109_update_database.php,日期和时间为2014-06-24,13:41:09);

  3. 再次运行迁移.

关于你对外键的评论......我不确定问题出在Laravel上.更有可能的是,它只是MySQL.

我避免使用外键,因为一旦你得到一组中等复杂的关系,就会开始遇到数据库一致性问题,就像你看到的那样 - 服务器很难弄清楚创建表和关系的顺序,以及它开始对转储文件(备份)等问题造成困难.

  • 避免外键会比拥有外键给你带来更多的问题。它会警告您有关不一致的情况,并防止您(或在同一项目中工作的任何其他开发人员)在进一步参与项目时最终陷入混乱。另外,如果你离开项目,他可能不得不从你离开的地方接起它,这真是可怜的人。我不会否决这个答案,因为外国的建议只是附注 (3认同)
  • 您可以将每个表的迁移拆分为两个迁移文件,一个用于表创建,另一个用于表关系,并将所有更新迁移日期保留在所有创建迁移日期之后。这样回滚和重新迁移时就不会出现问题。 (2认同)
  • 对于 Laravel 在所有创建完成后运行外键定义,这不是一个非常完美的解决方案吗? (2认同)

小智 9

您必须创建一个自定义命令,php artisan migrate:refresh --path=/database/migrations/name_migration.php该命令以您想要的顺序使用迁移的名称重复执行 。

像这样:

  1. 使用以下命令创建命令类: php artisan make:command NameClass
  2. 转到app/Console/Commands/并找到类文件NameClass.php
  3. 在 NameClass.php 中,您有两个属性$signature(命令的名称)和$description(有关您的命令功能的信息)。
  4. 设置命令的名称和描述。Ex: protected $signature='namecommand'; protected $descripton = 'This method migrate tables in order'
  5. 在 NameClass.php 中,您有一个名为 的方法handle(),您必须在此处声明您在编写命令时要执行的代码。
  6. 注册您的命令。转到app/Console/Kernel.php and将您的类添加到命令类列表中。 protected $commands = [ Commands\NameClass::class, ];
  7. 在终端中写入命令。 php artisan namecommand

例子:

  1. php artisan make:command MigrateInOrder

  2. 应用程序/控制台/命令/MigrateInOrder.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MigrateInOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate_in_order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Execute the migrations in the order specified in the file app/Console/Comands/MigrateInOrder.php \n Drop all the table in db before execute the command.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
       /** Specify the names of the migrations files in the order you want to 
        * loaded
        * $migrations =[ 
        *               'xxxx_xx_xx_000000_create_nameTable_table.php',
        *    ];
        */
        $migrations = [ 
                        '2020_04_18_005024_create_users_types.php',
                        '2014_10_12_000000_create_users_table.php',
                        '2014_10_12_100000_create_password_resets_table.php',
                        '2019_08_19_000000_create_failed_jobs_table.php'
        ];

        foreach($migrations as $migration)
        {
           $basePath = 'database/migrations/';          
           $migrationName = trim($migration);
           $path = $basePath.$migrationName;
           $this->call('migrate:refresh', [
            '--path' => $path ,            
           ]);
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)
  1. 转到 app/Console/Kernel.php 并注册您的命令
    protected $commands = [
        Commands\MigrateInOrder::class,
    ];
Run Code Online (Sandbox Code Playgroud)
  1. 执行命令
   php artisan migrate_in_order
Run Code Online (Sandbox Code Playgroud)


小智 5

受到 PhpMyAdmin 的启发,我将所有外键定义放在一个特定的远期文件中,例如:2999_12_31_235959_foreign_key.php

<?php

use App\Models\Post;
use App\Models\Tag;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class ForeignKeys extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // Post_tag
        Schema::table(Post::NOM, function (Blueprint $table) {
            $table->foreign('id_post')
                ->references('id_post')
                ->on(Post::NOM);

            $table->foreign('id_tag')
                ->references('id_tag')
                ->on(Tag::NOM);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我看到的唯一缺点是迁移中没有外键定义。

对于专业人士:

  • 保持数据库关系
  • 不关心表创建顺序