我在執行php artisan遷移時遇到此錯誤。我的遷移文件有問題嗎?或者有可能我的模型被錯誤編碼了?但是,即使模型中出現錯誤,遷移也應該起作用。SQLSTATE [HY000]:一般錯誤:1005無法創建表 - Laravel 4
[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');
}
}
好,我這樣做:)仍然得到同樣的錯誤。我用我的模型更新了我的問題。 – Gilko
樂隊是否遷移?或者沒有?樂隊應該先遷移,因爲gigs會引用它。 –
就是這樣。我更改了遷移文件的前綴。 – Gilko