SQLSTATE [HY000]:常规错误:1005无法创建表

编程入门 行业动态 更新时间:2024-10-26 06:38:37
本文介绍了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无法创建表

本文发布于:2023-10-26 01:15:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528646.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常规   错误   SQLSTATE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!