【问题标题】:Laravel migration works on Linux but not on windowsLaravel 迁移适用于 Linux,但不适用于 Windows
【发布时间】:2021-10-13 12:17:37
【问题描述】:

当我尝试在 Windows 上运行我的 Laravel 迁移时 这是在 Windows 上失败但在 Linux 上工作的迁移文件。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTenantOwnerBridgeTable extends Migration
{
    /**
     * Schema table name to migrate
     * @var string
     */
    public $tableName = 'tenant_owner_bridge';

    /**
     * Run the migrations.
     * @table tenant_owner_bridge
     *
     * @return void
     */
    public function up()
    {
        Schema::create($this->tableName, function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->unsignedInteger('tenant_owner_id');
            $table->string('tenant_id', 191);
            $table->unsignedBigInteger('primary_admin_id');

            $table->index(["primary_admin_id"], 'fk_tenant_owner_bridge_user_id_idx');

            $table->index(["tenant_id"], 'fk_tenant_owner_bridge_tenant_id_idx');

            $table->index(["tenant_owner_id"], 'fk_tenant_owner_bridge_tenant_owner_id_idx');
            $table->nullableTimestamps();


            $table->foreign('tenant_id', 'fk_tenant_owner_bridge_tenant_id_idx')
                ->references('id')->on('tenants')
                ->onDelete('no action')
                ->onUpdate('no action');

            $table->foreign('tenant_owner_id', 'fk_tenant_owner_bridge_tenant_owner_id_idx')
                ->references('id')->on('tenant_owners_info')
                ->onDelete('no action')
                ->onUpdate('no action');

            $table->foreign('primary_admin_id', 'fk_tenant_owner_bridge_user_id_idx')
                ->references('id')->on('users')
                ->onDelete('no action')
                ->onUpdate('no action');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
     public function down()
     {
       Schema::dropIfExists($this->tableName);
     }
}

这是引发的错误。 SQLSTATE[HY000]: 一般错误: 1005 Can't create table primehybrid.tenant_owner_bridge (errno: 150 "外键约束格式不正确") (SQL: alter table tenant_owner_bridge add constraint fk_tenant_owner_bridge_tenant_id_idx外键 (tenant_id) 引用 tenants (id) 删除无操作更新无操作)

这是外键引用的表。


<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTenantsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('tenants', function (Blueprint $table) {
            $table->string('id')->primary();

            // your custom columns may go here

            $table->timestamps();
            $table->json('data')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(): void
    {
        Schema::dropIfExists('tenants');
    }
}

有人能解释一下为什么它在 Linux 上运行而不是在 Windows 上运行,以及为什么它在 Windows 上失败。 另请参阅我的迁移文件列表:

2012_08_06_000003_create_account_statuses_table.php
2013_08_06_000004_create_countries_table.php
**2014_10_12_000000_create_users_table.php**
2014_10_12_100000_create_password_resets_table.php
2016_06_01_000001_create_oauth_auth_codes_table.php
2016_06_01_000002_create_oauth_access_tokens_table.php
2016_06_01_000003_create_oauth_refresh_tokens_table.php
2016_06_01_000004_create_oauth_clients_table.php
2016_06_01_000005_create_oauth_personal_access_clients_table.php
2018_08_08_100000_create_telescope_entries_table.php
2019_08_19_000000_create_failed_jobs_table.php
**2019_09_15_000010_create_tenants_table.php**
2019_09_15_000020_create_domains_table.php
2020_04_06_222004_create_sessions_table.php
2020_05_15_000010_create_tenant_user_impersonation_tokens_table.php
2020_08_10_170933_create_jobs_table.php
2021_04_30_174805_create_cache_table.php
**2021_08_06_000000_create_tenant_owners_info_table.php**
2021_08_06_000001_create_secret_questions_table.php
2021_08_06_000006_create_recovery_info_table.php
**2021_08_06_000007_create_tenant_owner_bridge_table.php**
2021_08_06_223928_create_permission_tables.php

【问题讨论】:

  • 迁移需要以正确的顺序运行,这是通过文件名完成的。如果您在 Windows 和 Linux 之间遇到问题,则可能是区分大小写的问题。相关的文件名是什么?
  • 2014_10_12_000000_create_users_table.php 2019_09_15_000010_create_tenants_table.php 2021_08_06_000000_create_tenant_owners_info_table.php 2021_08_06_000007_create_tenant_owner_table.php
  • 请注意,表之间的列定义也不匹配。
  • 尝试在foreign 方法中删除fk_tenant_owner_bridge_tenant_id_idx,使其类似于$table-&gt;foreign('tenant_id')-&gt;references('id')-&gt;on('tenants')-&gt;onDelete('no action')-&gt;onUpdate('no action');

标签: mysql laravel laravel-5 eloquent laravel-migrations


【解决方案1】:

我不知道你为什么要添加index() 方法,你可以通过编写类似的东西来简单地创建一个外部引用

对于 Laravel > 5.5

$table->unsignedBigInteger('primary_admin_id');
$table->foreign('primary_admin_id')
             ->references('id')
             ->on('users')
             ->onUpdate('no action')
             ->onDelete('no action');

对于 Laravel 8

$table->foreignId('primary_admin_id')->
             ->references('id')
             ->on('users')
             ->onUpdate('no action');
             ->onDelete('no action');

另外,您需要记住,您的迁移应该按正确的顺序进行,例如,如果您有一个 item_order 表,您应该拥有所有必需的表,这些表将在 in中有引用> item_order 表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2021-06-11
    • 2016-03-01
    • 2021-05-02
    • 2017-06-14
    相关资源
    最近更新 更多