代码之家  ›  专栏  ›  技术社区  ›  Vasim Shaikh

迁移时表中的FK不工作

  •  0
  • Vasim Shaikh  · 技术社区  · 6 年前

    当我在那时为表创建迁移时,它的through-following错误有问题。

    照明\数据库\查询异常:sqlstate[hy000]:常规 错误:1005无法创建表 yourwebs_veridocedu . #sql-2c46_8e (errno:150“外键约束格式不正确”)(sql: 更改表 user_role_mappings 添加约束 user_role_mappings_user_id_foreign 外键( user_id ) 参考文献 users ( id )删除时层叠)

    用户角色映射表的迁移:

    public function up()
        {
            Schema::create('user_role_mappings', function (Blueprint $table) {
                $table->increments('id');
    
                $table->integer('user_id');
                $table->integer('group_id');
                $table->integer('roleid');
                $table->integer('status');
                $table->integer('createdby');
                $table->integer('modifiedby');
                $table->string('publicguid');
                $table->string('privateguid');
                $table->timestamps();
    
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
                $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
            });
        }
    

    角色表迁移:

    public function up()
    {
        Schema::create('userroles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('rolename');
             $table->integer('status')->default(1);
            $table->integer('createdby')->default(1);
            $table->integer('modifiedby')->default(1);
            $table->string('publicguid');
            $table->string('privateguid');
            $table->timestamps(); 
    
        });
    }
    

    用户表迁移:

        <?php
    
    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateUsersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->engine = 'InnoDB';
                $table->increments('id');
    
    
                $table->string('name');
                $table->string('email',150)->unique();
                $table->string('password');
                $table->integer('roleid')->unsigned();
                $table->rememberToken();
    
                /*Common Fields*/
                $table->integer('status');
                $table->integer('createdby');
                $table->integer('modifiedby');
                $table->string('publicguid');
                $table->string('privateguid');
                $table->timestamps();
    
               /*From other table */
                $table->foreign('roleid') ->references('id')->on('userroles')->onDelete('cascade');
    
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
    
        public function down()
        {
            Schema::dropIfExists('users');
        }
    }
    

    表中的FK不工作,无法迁移。

    3 回复  |  直到 6 年前
        1
  •  0
  •   Jonas Staudenmeir    6 年前

    InnoDB 引擎和列类型必须匹配:

    Schema::create('user_role_mappings', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->unsignedInteger('user_id');
        $table->unsignedInteger('group_id');
    });
    
        2
  •  1
  •   Jerodev    6 年前

    increments

    $table->integer('user_id')->unsigned();
    $table->integer('group_id')->unsigned();
    
        3
  •  1
  •   Rahul    6 年前

    尝试下面给出的方法。

    $table->integer('user_id')->unsigned()->index();
    $table->integer('group_id')->unsigned()->index();