当我做php artisan migrate时,我收到此错误。我的迁移文件有什么问题吗?或者我的模型可能编码错误吗?但即使模型出现问题,迁移也应该有效吗?
[Exception]
SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-
16643_2033' (errno: 150) (SQL: alter table `gigs` add constraint gigs_band_
id_foreign foreign key (`band_id`) references `bands` (`band_id`) on delete
cascade) (Bindings: array (
))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-
16643_2033' (errno: 150)
演出迁移
public function up()
{
Schema::create('gigs', function($table)
{
$table->increments('gig_id');
$table->dateTime('gig_startdate');
$table->integer('band_id')->unsigned();
$table->integer('stage_id')->unsigned();
$table->foreign('band_id')
->references('band_id')->on('bands')
->onDelete('cascade');
$table->foreign('stage_id')
->references('stage_id')->on('stages')
->onDelete('cascade');
});
public function down()
{
Schema::table('gigs', function($table)
{
Schema::drop('gigs');
$table->dropForeign('gigs_band_id_foreign');
$table->dropForeign('gigs_stage_id_foreign');
});
}
乐队迁移
public function up()
{
Schema::create('bands', function($table)
{
$table->increments('band_id');
$table->string('band_name');
$table->text('band_members');
$table->string('band_genre');
$table->dateTime('band_startdate');
});
}
public function down()
{
Schema::table('bands', function(Blueprint $table)
{
Schema::drop('bands');
});
}
模型乐队
<?php
class Band extends Eloquent {
protected $primaryKey = 'band_id';
public function gig()
{
return $this->hasOne('Gig', 'band_id', 'band_id');
}
}
模特演出
<?php
class Gig extends Eloquent {
protected $primaryKey = 'gig_id';
public function gig()
{
return $this->belongsTo('Band', 'band_id', 'band_id');
}
public function stage()
{
return $this->belongsTo('Stage', 'stage_id', 'stage_id');
}
}
您必须首先创建表,然后创建外键:
Schema::create('gigs', function($table)
{
$table->increments('gig_id');
$table->dateTime('gig_startdate');
$table->integer('band_id')->unsigned();
$table->integer('stage_id')->unsigned();
});
Schema::table('gigs', function($table)
{
$table->foreign('band_id')
->references('band_id')->on('bands')
->onDelete('cascade');
$table->foreign('stage_id')
->references('stage_id')->on('stages')
->onDelete('cascade');
});
并且你的bands
表应首先迁移,因为gigs
正在引用它。
虽然这不适用于OP,但其他人可能会遇到此问题:
从Laravel Schema docs的底部:
注意:创建引用递增整数的外键时,请记住始终使外键列无符号。
在迁移文件中创建表时,可以通过$table->integer('user_id')->unsigned();
执行此操作。
花了几分钟时间才意识到发生了什么。
对于那些其他答案没有帮助的人,当您尝试在非可空列上使用'SET_NULL'
操作时,同样的错误也会抛出。
对于仍然遇到此问题的用户,请确保您设置为外键的字段是主键或应该是唯一的,因为外键只能是主键或唯一字段。见下面的示例
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username',25)->unique();
});
Schema::create('another_table', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 25);
$table->foreign('name')->references('username')->on('users')
});
$table->integer('band_id')->unsigned();
$table->integer('stage_id')->unsigned();
在laravel 5.8中,users_table使用bigIncrements('id')数据类型作为主键。因此,当您想要引用外键约束band_id时,stage_id列需要是unsignedBigInteger('stage_id')和band_id类型。
经理也测试了这种方式。
这似乎是一般的外键问题错误。对我来说,当我在references
和on
方法中切换参数时,我得到了它。我有:
$table->foreign('user_id')->references('users')->on('id');
代替:
$table->foreign('user_id')->references('id')->on('users');
我只是通过指向Laravel使用unique()
指出该字段是唯一的来解决这个问题。
例:
Schema::create('branches', function (Blueprint $table) {
$table->increments('id');
/* This fields serves as foriegn key on functions
table and it must follow the database **key rules**
by being unique */
$table->string('branch_code')->unique();
$table->string('branch_name');
$table->string('branch_address');
$table->timestamps();
});
功能表:
Schema::create('functions', function (Blueprint $table) {
$table->increments('id');
$table->string('branch_code');
$table->string('function');
$table->timestamps();
//Reference to branch_code on branches table
$table->foreign('branch_code')->references('branch_code')->on('branches');
});
我在config / database 'engine' => 'InnoDB',
中更改为'engine' => null,
并且正常工作