Laravel迁移问题 一般错误: 3780

问题描述 投票:0回答:1

我有一个开发人员,几乎放弃了一个项目。 我有服务器上的代码,我试图让它在我的本地环境中工作。 我试图让它迁移,但我一直得到一个引用的问题,我已经看了帖子,并尝试了一切,从添加disableForeignKeyConstraints();到composer dump-autoload等。 但还是一直得到

SQLSTATE[HY000]。一般错误。3780 在外键约束'role_user_user_id_foreign'中引用列'user_id'和被引用列'id'不兼容。(SQL: 改变表 role_user 约束 role_user_user_id_foreign 外键user_id) 参考文献 users (id)上删除级联)

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');
    }
}
mysql laravel migration
1个回答
0
投票

貌似你的role_user表需要你的用户表先被创建,让我们假设你的迁移文件是这样的。

1) create_users_table_migration.php2) create_roles_table_migration.php。

它们将被存储在laravelProjectdatabasemigrations中。

试着通过以下命令只运行用户迁移。

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

然后通过.NET迁移你的user_roles表。

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

0
投票

这些项目没有成功,因为我有一个数据库的副本,所以我做了一个反向迁移 在这里找到了迁移生成器的信息

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

© www.soinside.com 2019 - 2024. All rights reserved.