Laravel migration (errno: 150 “Foreign key constraint is incorrectly formed”)

后端 未结 18 2461
谎友^
谎友^ 2020-12-05 14:15

I have an orders table and a have a sell_shipping_labels which references orders.id as a foreign. However when I run the Laravel migration I get th

18条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 15:08

    [![enter image description here][1]][1]
    public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
    
    
            });
        }
    
    I changed $table->bigIncrements('id') to $table->Increments('id')
    For this user_id of files table become same integer type as user table field id. After this command worked.
    
       public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
            });
        }
    
    
    
    For the second table
     {
            Schema::create('files', function (Blueprint $table) {
                $table->increments('id');
    
    });
    
                Schema::table('files', function($table) {
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
               });
        }
    

提交回复
热议问题