![](/img/trans.png)
[英]SQLSTATE[HY000]: General error: 1005 Can't create table Laravel 8
[英]SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4
我在執行 php artisan 遷移時收到此錯誤。 我的遷移文件有問題嗎? 還是我的模型編碼錯誤? 但是即使模型中有問題,遷移也應該起作用?
[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 文檔的底部:
注意:創建引用遞增整數的外鍵時,請記住始終使外鍵列無符號。
你可以通過$table->integer('user_id')->unsigned();
在遷移文件中創建表時。
我花了幾分鍾才意識到發生了什么。
對於那些其他答案沒有幫助的人,當您嘗試對不可為空的列使用'SET_NULL'
操作時,也會引發相同的錯誤。
以上沒有對我有用! 但是這樣做了:我只是將整數更改為 unsignedInteger
$table->unsignedBigInteger('user_id')->unsigned();
對於那些仍然遇到此問題的人,請確保您設置為外鍵的字段是主鍵或應該是唯一的,因為外鍵只能是主字段或唯一字段。 請參閱下面的示例
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 類型。
經理也這樣測試過。
我有一個類似的問題。 您必須使用 bigInteger 來解決此問題:
$table->bigInteger('user_id')->unsigned();
這似乎是一般的外鍵問題錯誤。 對我來說,當我切換references
和方法on
的參數時,我得到了它。 我有:
$table->foreign('user_id')->references('users')->on('id');
代替:
$table->foreign('user_id')->references('id')->on('users');
我剛剛通過使用unique()
指向 Laravel 該字段是唯一的來解決這個問題。
例子:
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,
並且可以正常工作
您可以使用
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('users')->on('id');
BigInt 的數據類型為 bigint(20),而整數的數據類型為 int(10)。 雖然這兩個都是無符號整數,但“長度/值”不匹配。
對於 Laravel 6.X,使用這種格式。
$table->unsignedBigInteger('dest_id')->unsigned();
$table->foreign('dest_id')->references('id')->on('destinations')->onDelete('cascade');
變化自:
$table->unsignedInteger('user_id')->unique();
至:
$table->unsignedBigInteger('user_id')->unique();
為我工作
外鍵必須使用 unsignedInterger 或 unsignedBigInterger
考試:
public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('post_id');
$table->string('post_type');
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->timestamps();
});
}
您還可以為約束的“on delete”和“on update”屬性指定所需的操作:
$table->foreignId('user_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');
我認為您應該檢查引用的表,例如,如果您傳遞值,您可能會收到此類錯誤。 這可能是$table->foreign('course_id')->references('course_id')->on('ptable'); which is wrong but
$table->foreign('course_id')->references('course_id')->on('ptable'); which is wrong but
$table->foreign('course_id')->references('level_id')->on('ptable');` 是正確的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.