php SQLSTATE[HY000]:一般错误:1005 无法创建表 - Laravel 4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20999161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4
提问by Gilko
I get this error when I do php artisan migrate. Is there something wrong in my migration files? Or is it possible my models are wrong coded? But the migrations should work even there is something wrong in the models?
当我执行 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)
gigs migration
演出迁移
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');
});
}
bands migration
波段迁移
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');
});
}
Model Band
模特乐队
<?php
class Band extends Eloquent {
protected $primaryKey = 'band_id';
public function gig()
{
return $this->hasOne('Gig', 'band_id', 'band_id');
}
}
Model Gig
模型演出
<?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');
}
}
回答by Antonio Carlos Ribeiro
You must first create the table, then create the foreign keys:
您必须首先创建表,然后创建外键:
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');
});
And your bandstable should migrate first, since the gigsis referencing it.
并且您的bands表应该首先迁移,因为它gigs正在引用它。
回答by Andrew
While this doesn't apply to OP, others might have this issue:
虽然这不适用于 OP,但其他人可能会遇到此问题:
From the bottom of the Laravel Schema docs:
从Laravel Schema 文档的底部:
Note: When creating a foreign key that references an incrementing integer, remember to always make the foreign key column unsigned.
注意:创建引用递增整数的外键时,请记住始终使外键列无符号。
You can do this via $table->integer('user_id')->unsigned();when creating your table in the migration file.
您可以通过$table->integer('user_id')->unsigned();在迁移文件中创建表来执行此操作。
Took me a few minutes to realize what was happening.
我花了几分钟才意识到发生了什么。
回答by Kamoris
For those whom other answers doesn't help, the same error throws also when you try to use 'SET_NULL'action on non-nullable column.
对于那些其他答案没有帮助的人,当您尝试'SET_NULL'对不可为空的列使用操作时,也会抛出相同的错误。
回答by David Kruger
回答by AddTek
for those who are still having this issue, make sure the field you setting as foreign key is a primary key or should be unique since a foreign key can only be a primary or unique field. see sample below
对于那些仍然有此问题的人,请确保您设置为外键的字段是主键或应该是唯一的,因为外键只能是主字段或唯一字段。请参阅下面的示例
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')
});
回答by Goldman.Vahdettin
$table->integer('band_id')->unsigned();
$table->integer('stage_id')->unsigned();
In laravel 5.8, the users_table uses bigIncrements('id') data type for the primary key. So that when you want to refer a foreign key constraint your band_id,stage_id column needs to be unsignedBigInteger('stage_id') and band_id type.
在 laravel 5.8 中, users_table 使用 bigIncrements('id') 数据类型作为主键。因此,当您想引用外键约束时,您的 band_id,stage_id 列需要是 unsignedBigInteger('stage_id') 和 band_id 类型。
The manager also tested this way.
经理也是这样测试的。
回答by Ritwik Math
You can use
您可以使用
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('users')->on('id');
BigInt has datatype of bigint(20) but integer has datatype of int(10). Though both of those are unsigned integer, 'Length/Values' does not match.
BigInt 的数据类型为 bigint(20),而 integer 的数据类型为 int(10)。尽管这两个都是无符号整数,但“长度/值”不匹配。
回答by falaFulani
For Laravel 6.X use this format.
对于 Laravel 6.X 使用这种格式。
$table->unsignedBigInteger('dest_id')->unsigned();
$table->foreign('dest_id')->references('id')->on('destinations')->onDelete('cascade');
回答by Omar Chaabouni
Nothing of the above worked for me ! But this did : I just changed integer to unsignedInteger
以上都不适合我!但是这样做了:我只是将整数更改为无符号整数
$table->unsignedBigInteger('user_id')->unsigned();
回答by ajon
This seems to be a general foreign key issue error. For me I got it when I switched the arguments in the referencesand onmethods. I had:
这似乎是一个一般的外键问题错误。对我来说,当我切换references和on方法中的参数时,我得到了它。我有:
$table->foreign('user_id')->references('users')->on('id');
instead of:
代替:
$table->foreign('user_id')->references('id')->on('users');

