【问题标题】:Laravel Migration issue General Error: 3780Laravel 迁移问题一般错误:3780
【发布时间】:2020-08-25 07:38:39
【问题描述】:

我有一个开发人员几乎放弃了一个项目。我有来自服务器的代码,我正试图让它在我的本地环境中运行。我正在尝试让它迁移,但我一直遇到引用问题,我查看了帖子并尝试了从添加 disableForeignKeyConstraints();给作曲家转储自动加载等。但仍然不断得到

SQLSTATE[HY000]:一般错误:3780 在外键约束“role_user_user_id_foreign”中引用列“user_id”和引用列“id”不兼容。 (SQL:修改表role_user添加约束role_user_user_id_foreign外键(user_id)在删除级联时引用usersid

SQLSTATE[HY000]:一般错误:3780 在外键约束“role_user_user_id_foreign”中引用列“user_id”和引用列“id”不兼容。

我看到它说不兼容所以我尝试添加 unsigned();到用户创建,但仍然没有骰子。

<?php

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

class CreateRoleUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::disableForeignKeyConstraints();
        Schema::create('role_user', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->integer('role_id')->unsigned()->index();
            $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->timestamps();
        });
        Schema::enableForeignKeyConstraints();
    }

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

和用户创建迁移

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('name')->unique();
            $table->string('first_name')->nullable();
            $table->string('last_name')->nullable();
            $table->string('email')->unique()->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->boolean('activated')->default(false);
            $table->string('token');
            $table->ipAddress('signup_ip_address')->nullable();
            $table->ipAddress('signup_confirmation_ip_address')->nullable();
            $table->ipAddress('signup_sm_ip_address')->nullable();
            $table->ipAddress('admin_ip_address')->nullable();
            $table->ipAddress('updated_ip_address')->nullable();
            $table->ipAddress('deleted_ip_address')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

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

【问题讨论】:

  • 您的users 表是在您的role_user 表之前创建的吗?两个迁移文件的名称是什么(包括时间戳)?
  • 是的用户是第一个 2014_10_12_000000_create_user_table.php 和 2016_01_15_105324_create_role_user_table.php
  • 确实很奇怪.. 如果你去掉你的外国人,它运行正常吗?
  • 尝试用$table-&gt;bigInteger('user_id')-&gt;unsigned()-&gt;index();替换$table-&gt;integer('user_id')-&gt;unsigned()-&gt;index();

标签: mysql laravel migration


【解决方案1】:

看起来您的 role_user 表需要先创建您的用户表。 假设您的迁移文件如下所示:

1) create_users_table_migration.php 2) create_roles_table_migration.php

它们将存储在 laravelProject/database/migrations 中

尝试通过以下命令仅运行您的用户迁移:

php artisan migrate --path=laravelProject/database/migrations/create_users_table_migration.php

然后通过以下方式迁移您的 user_roles 表:

php artisan migrate --path=laravelProject/database/migrations/create_users_table_migration.php

【讨论】:

  • 首先创建用户 2014_10_12(用户前缀)与角色 2016_01_15
【解决方案2】:

这些项目不起作用,因为我有一个数据库副本,所以我进行了反向迁移info found here for migration generator

即使我遇到了类问题,我只是从开发人员使用的供应商文件夹中删除了迁移文件,然后就能够运行新的迁移。谢谢大家!

【讨论】:

    猜你喜欢
    • 2020-01-15
    • 2015-04-09
    • 2020-05-13
    • 2021-03-29
    • 2020-03-19
    • 2019-11-21
    • 2020-07-08
    • 2015-12-14
    • 2017-01-31
    相关资源
    最近更新 更多