SQLSTATE[HY000]: General error: 1005 Can#39;t create table - Laravel 4(SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4)
问题描述
我在执行 php artisan 迁移时收到此错误.我的迁移文件有问题吗?还是我的模型编码错误?但是即使模型中有问题,迁移也应该可以工作?
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?
[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');
}
}
推荐答案
必须先创建表,再创建外键:
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');
});
并且您的 bands
表应该首先迁移,因为 gigs
正在引用它.
And your bands
table should migrate first, since the gigs
is referencing it.
这篇关于SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4
基础教程推荐
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在多维数组中查找最大值 2021-01-01