Kry*_*ten 52
回滚所有迁移(或从新数据库开始);
更改构成迁移文件名第一部分的日期,使其符合您的要求(例如2014_06_24_134109_update_database.php,日期和时间为2014-06-24,13:41:09);
再次运行迁移.
关于你对外键的评论......我不确定问题出在Laravel上.更有可能的是,它只是MySQL.
我避免使用外键,因为一旦你得到一组中等复杂的关系,就会开始遇到数据库一致性问题,就像你看到的那样 - 服务器很难弄清楚创建表和关系的顺序,以及它开始对转储文件(备份)等问题造成困难.
小智 9
您必须创建一个自定义命令,php artisan migrate:refresh --path=/database/migrations/name_migration.php该命令以您想要的顺序使用迁移的名称重复执行
。
像这样:
php artisan make:command NameClassapp/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
应用程序/控制台/命令/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)
protected $commands = [
Commands\MigrateInOrder::class,
];
Run Code Online (Sandbox Code Playgroud)
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)
我看到的唯一缺点是迁移中没有外键定义。
对于专业人士:
| 归档时间: |
|
| 查看次数: |
16967 次 |
| 最近记录: |