Laravel 更改迁移顺序
Laravel change migration order
有没有一种方法可以更改迁移顺序而无需重新制作它们?因为现在我的外键有问题 -_- (working with laravel)
Roll back all the migrations(或从新数据库开始);
更改构成迁移文件名第一部分的日期,以便它们按您想要的顺序排列(例如,对于 2014_06_24_134109_update_database.php
,日期和时间是 2014-06- 24, 13:41:09);
运行 再次迁移。
关于您关于外键的评论...我不确定问题出在 Laravel 上。更有可能的是 MySQL.
我避免使用外键,因为一旦你获得了一组中等复杂的关系,你就会开始 运行 遇到数据库一致性问题,就像你所看到的那样 - 服务器很难确定要执行的顺序在其中创建表和关系,它开始对转储文件(用于备份)等问题造成困难。
您必须创建一个自定义命令来执行
php artisan migrate:refresh --path=/database/migrations/name_migration.php
按您想要的顺序重复使用迁移的名称。
像这样:
- 创建命令 class 具有:
php artisan make:command NameClass
- 转到
app/Console/Commands/
并找到 class 文件 NameClass.php
- 在 NameClass.php 中,您有两个属性
$signature
(命令的名称)和 $description
(有关您的命令的功能的信息)。
- 设置命令的名称和描述。
Ex: protected $signature='namecommand'; protected $descripton = 'This method migrate tables in order'
- 在NameClass.php里面你有一个方法叫做
handle()
,在这里你必须在你写命令的时候声明你想要执行的代码。
- 注册您的命令。转到
app/Console/Kernel.php and
将您的 class 添加到命令列表 类。
protected $commands = [
Commands\NameClass::class,
];
- 在终端中写入命令。
php artisan namecommand
示例:
php artisan make:command MigrateInOrder
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 ,
]);
}
}
}
- 转到 app/Console/Kernel.php 并注册您的命令
protected $commands = [
Commands\MigrateInOrder::class,
];
- 执行命令
php artisan migrate_in_order
您只需更改迁移顺序。如果乐队或舞台 table 低于用户 table,MySQL 找不到参考。 =)
最好和最简单的方法就是重命名迁移 yyyy_mm_dd_hhmmss_migration_name
。如果您的迁移遵循此顺序,Laravel 将确保 运行 迁移按日期形式排序,
受 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);
});
}
}
我看到的唯一缺点是迁移中没有外键定义。
专业人士:
- 保持数据库关系
- 不关心table创建顺序
基于 Galeokerdo 的回答,建议为外键创建一个单独的迁移文件,并将日期放在遥远的未来,我尝试了它并且效果很好。但后来我开始考虑回滚。事实证明,Laravel 在回滚迁移时采用相反的顺序。即先回滚最新的文件
由于回滚将无法使用外键约束,我尝试将我的 foreign-key-removal 代码放在单独的外键迁移的“向下”方法中,发现该文件将执行首先在所有其他迁移文件之前。像这样:
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tablename', function (Blueprint $table) {
$table->dropForeign('tablename_foreignkey_foreign');
});
}
"tablename_foreignkey_foreign" is the name of the foreign key constraint. By default, it is
"nameofthetable_foreignkeycolumn_foreign"
我只是想分享这个,以防有人像我一样苦苦挣扎。
有没有一种方法可以更改迁移顺序而无需重新制作它们?因为现在我的外键有问题 -_- (working with laravel)
Roll back all the migrations(或从新数据库开始);
更改构成迁移文件名第一部分的日期,以便它们按您想要的顺序排列(例如,对于
2014_06_24_134109_update_database.php
,日期和时间是 2014-06- 24, 13:41:09);运行 再次迁移。
关于您关于外键的评论...我不确定问题出在 Laravel 上。更有可能的是 MySQL.
我避免使用外键,因为一旦你获得了一组中等复杂的关系,你就会开始 运行 遇到数据库一致性问题,就像你所看到的那样 - 服务器很难确定要执行的顺序在其中创建表和关系,它开始对转储文件(用于备份)等问题造成困难。
您必须创建一个自定义命令来执行
php artisan migrate:refresh --path=/database/migrations/name_migration.php
按您想要的顺序重复使用迁移的名称。
像这样:
- 创建命令 class 具有:
php artisan make:command NameClass
- 转到
app/Console/Commands/
并找到 class 文件NameClass.php
- 在 NameClass.php 中,您有两个属性
$signature
(命令的名称)和$description
(有关您的命令的功能的信息)。 - 设置命令的名称和描述。
Ex: protected $signature='namecommand'; protected $descripton = 'This method migrate tables in order'
- 在NameClass.php里面你有一个方法叫做
handle()
,在这里你必须在你写命令的时候声明你想要执行的代码。 - 注册您的命令。转到
app/Console/Kernel.php and
将您的 class 添加到命令列表 类。protected $commands = [ Commands\NameClass::class, ];
- 在终端中写入命令。
php artisan namecommand
示例:
php artisan make:command MigrateInOrder
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 ,
]);
}
}
}
- 转到 app/Console/Kernel.php 并注册您的命令
protected $commands = [
Commands\MigrateInOrder::class,
];
- 执行命令
php artisan migrate_in_order
您只需更改迁移顺序。如果乐队或舞台 table 低于用户 table,MySQL 找不到参考。 =)
最好和最简单的方法就是重命名迁移 yyyy_mm_dd_hhmmss_migration_name
。如果您的迁移遵循此顺序,Laravel 将确保 运行 迁移按日期形式排序,
受 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);
});
}
}
我看到的唯一缺点是迁移中没有外键定义。
专业人士:
- 保持数据库关系
- 不关心table创建顺序
基于 Galeokerdo 的回答,建议为外键创建一个单独的迁移文件,并将日期放在遥远的未来,我尝试了它并且效果很好。但后来我开始考虑回滚。事实证明,Laravel 在回滚迁移时采用相反的顺序。即先回滚最新的文件
由于回滚将无法使用外键约束,我尝试将我的 foreign-key-removal 代码放在单独的外键迁移的“向下”方法中,发现该文件将执行首先在所有其他迁移文件之前。像这样:
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tablename', function (Blueprint $table) {
$table->dropForeign('tablename_foreignkey_foreign');
});
}
"tablename_foreignkey_foreign" is the name of the foreign key constraint. By default, it is
"nameofthetable_foreignkeycolumn_foreign"
我只是想分享这个,以防有人像我一样苦苦挣扎。