Laravel 4 Migrations throwing 1072 error

 ̄綄美尐妖づ 提交于 2019-12-06 02:52:20

问题


I have a couple of migrations in a new Laravel 4 project. One is for regions and the other for areas. Each region has a number of areas, and areas belong to regions.

I have used Laravel 4 and the migration functions on a number of occasions but have never come accross this issue before. When I run php artisan migrate:install followed by php artisan migrate I get the following error:

$ php artisan migrate
  [Exception]
  SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'region_
  id' doesn't exist in table (SQL: alter table `areas` add constraint areas_r
  egion_id_foreign foreign key (`region_id`) references `regions` (`id`)) (Bi
  ndings: array (
  ))
migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="...
"]] [--pretend] [--seed]

// The regions migration

class CreateRegionsTable extends Migration {

 /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    // Creates the regions table
   Schema::create('regions', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->string('name', 160)->unique();
        $table->timestamps();
    });
  }
}

// The areas migration

class CreateAreasTable extends Migration {

 /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    // Creates the cemeteries table
    Schema::create('areas', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->foreign('region_id')->references('id')->on('regions');
        $table->string('name', 160)->unique();
        $table->timestamps();
    });
  }
}

回答1:


You have to create the column related to the foreign key:

class CreateAreasTable extends Migration {

 /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    // Creates the cemeteries table
    Schema::create('areas', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');

        $table->integer('region_id')->unsigned();
        $table->foreign('region_id')->references('id')->on('regions');

        $table->string('name', 160)->unique();
        $table->timestamps();

    });
  }
}

Sometimes (depends on your database server) you'll have to create your foreign keys in two steps:

class CreateAreasTable extends Migration {

 /**
  * Run the migrations.
  *
  * @return void
  */
  public function up()
  {
    // Create the table and the foreign key column
    Schema::create('areas', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');

        $table->integer('region_id')->unsigned();

        $table->string('name', 160)->unique();
        $table->timestamps();

    });

    // Create the relation
    Schema::tabe('areas', function($table)
    {
        $table->foreign('region_id')->references('id')->on('regions');
    });
  }
}



回答2:


and don't forget to make the foreign keys unsigned.

         $table->integer('region_id')->unsigned();


来源:https://stackoverflow.com/questions/16928032/laravel-4-migrations-throwing-1072-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!