【发布时间】:2015-12-08 01:15:23
【问题描述】:
有没有一种方法可以在不重新制作迁移顺序的情况下更改它们?因为现在我的外键有问题-_-(使用 laravel)
【问题讨论】:
有没有一种方法可以在不重新制作迁移顺序的情况下更改它们?因为现在我的外键有问题-_-(使用 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);
});
}
}
我看到的唯一缺点是在迁移中没有外键定义。
对于专业人士:
【讨论】:
最好和最简单的方法是将迁移重命名为yyyy_mm_dd_hhmmss_migration_name。如果你的迁移遵循这个顺序,Laravel 将确保运行迁移是按日期排序的,
【讨论】:
您只需要更改迁移顺序。如果乐队或舞台表低于用户表,MySQL 找不到参考。 =)
【讨论】:
Roll back all the migrations(或从新数据库开始);
更改构成迁移文件名第一部分的日期,使其按您想要的顺序排列(例如,对于2014_06_24_134109_update_database.php,日期和时间为 2014-06-24, 13:41:09);
再次运行迁移。
关于您对外键的评论...我不确定问题出在 Laravel 上。更有可能的是,它只是 MySQL。
我避免使用外键,因为一旦你得到一组相对复杂的关系,你就会开始遇到像你所看到的那样的数据库一致性问题 - 服务器很难弄清楚创建表和关系的顺序,并且它开始导致转储文件(用于备份)等问题。
【讨论】:
mysqldump 之类的工具 - 如果您有一堆表之间存在关系,mysqldump 通常以无法成功重新导入的顺序写出它们(例如,表 A 依赖于表 B,表 B 依赖于表 A)。也许近年来情况发生了变化,所以 YMMV。
您必须创建一个执行的自定义命令
php artisan migrate:refresh --path=/database/migrations/name_migration.php 按照您想要的顺序重复使用迁移的名称。
像这样:
php artisan make:command NameClass
app/Console/Commands/找到类文件NameClass.php
$signature(命令的名称)和 $description(有关命令执行的信息)。 Ex: protected $signature='namecommand'; protected $descripton = 'This method migrate tables in order'
handle()的方法,在这里你必须声明你要在编写命令时执行的代码。app/Console/Kernel.php and 将您的类添加到命令类列表中。
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 ,
]);
}
}
}
protected $commands = [
Commands\MigrateInOrder::class,
];
php artisan migrate_in_order
【讨论】: