【问题标题】:Laravel change migration orderLaravel 更改迁移顺序
【发布时间】:2015-12-08 01:15:23
【问题描述】:

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

【问题讨论】:

    标签: laravel-5 migration


    【解决方案1】:

    从 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);
            });
        }
    }
    

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

    对于专业人士:

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

    【讨论】:

      【解决方案2】:

      最好和最简单的方法是将迁移重命名为yyyy_mm_dd_hhmmss_migration_name。如果你的迁移遵循这个顺序,Laravel 将确保运行迁移是按日期排序的,

      【讨论】:

        【解决方案3】:

        您只需要更改迁移顺序。如果乐队或舞台表低于用户表,MySQL 找不到参考。 =)

        【讨论】:

          【解决方案4】:
          1. Roll back all the migrations(或从新数据库开始);

          2. 更改构成迁移文件名第一部分的日期,使其按您想要的顺序排列(例如,对于2014_06_24_134109_update_database.php,日期和时间为 2014-06-24, 13:41:09);

          3. 再次运行迁移。

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

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

          【讨论】:

          • 这是一个很好的小技巧,它拯救了我的一天。谢谢!
          • 是的,这也是我的问题。谢谢:)
          • 您可以将每个表的迁移拆分为两个迁移文件,一个用于创建表,另一个用于表关系,并在所有创建迁移之后保留所有更新迁移的日期。这样在回滚和重新迁移时没有问题。
          • 在所有创建完成后运行外键定义不是非常完美的解决方案吗?
          • 这可能适用于 Laravel,但我在外键定义方面看到的问题是使用 mysqldump 之类的工具 - 如果您有一堆表之间存在关系,mysqldump 通常以无法成功重新导入的顺序写出它们(例如,表 A 依赖于表 B,表 B 依赖于表 A)。也许近年来情况发生了变化,所以 YMMV。
          【解决方案5】:

          您必须创建一个执行的自定义命令 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. app/Console/Commands/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 ,            
                     ]);
                  }
              }
          } 
          
          1. 转到 app/Console/Kernel.php 并注册您的命令
              protected $commands = [
                  Commands\MigrateInOrder::class,
              ];
          
          1. 执行命令
             php artisan migrate_in_order
          

          【讨论】:

            猜你喜欢
            • 2015-10-09
            • 1970-01-01
            • 2017-11-24
            • 2019-06-09
            • 1970-01-01
            • 2016-02-26
            • 2018-11-14
            • 2013-12-18
            • 2021-07-01
            相关资源
            最近更新 更多