Laravel 4 Migrations抛出1072错误 [英] Laravel 4 Migrations throwing 1072 error

查看:69
本文介绍了Laravel 4 Migrations抛出1072错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个新的Laravel 4项目中进行了几次迁移.一种用于区域,另一种用于区域.每个区域都有许多区域,而区域属于区域.

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.

我已经多次使用过Laravel 4和迁移功能,但以前从未遇到过此问题.当我依次运行php artisan migrate:installphp artisan migrate时,出现以下错误:

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]

//区域迁移

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();
    });
  }
}

//区域迁移

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();
    });
  }
}

推荐答案

您必须创建与外键相关的列:

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');
    });
  }
}

这篇关于Laravel 4 Migrations抛出1072错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆