gpt4 book ai didi

php - Laravel 4 迁移抛出 1072 错误

转载 作者:可可西里 更新时间:2023-11-01 06:53:58 26 4
gpt4 key购买 nike

我在新的 Laravel 4 项目中进行了几次迁移。一个用于区域,另一个用于区域。每个区域都有若干个区域,区域归区域。

我曾多次使用 Laravel 4 和迁移功能,但以前从未遇到过这个问题。当我运行 php artisan migrate:install 后跟 php artisan migrate 我收到以下错误:

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

最佳答案

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

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

});
}
}

有时(取决于您的数据库服务器)您必须分两步创建外键:

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

关于php - Laravel 4 迁移抛出 1072 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16928032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com