• <legend id='1PhWC'><style id='1PhWC'><dir id='1PhWC'><q id='1PhWC'></q></dir></style></legend>
    <i id='1PhWC'><tr id='1PhWC'><dt id='1PhWC'><q id='1PhWC'><span id='1PhWC'><b id='1PhWC'><form id='1PhWC'><ins id='1PhWC'></ins><ul id='1PhWC'></ul><sub id='1PhWC'></sub></form><legend id='1PhWC'></legend><bdo id='1PhWC'><pre id='1PhWC'><center id='1PhWC'></center></pre></bdo></b><th id='1PhWC'></th></span></q></dt></tr></i><div id='1PhWC'><tfoot id='1PhWC'></tfoot><dl id='1PhWC'><fieldset id='1PhWC'></fieldset></dl></div>

    1. <small id='1PhWC'></small><noframes id='1PhWC'>

    2. <tfoot id='1PhWC'></tfoot>

          <bdo id='1PhWC'></bdo><ul id='1PhWC'></ul>

        SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4

        SQLSTATE[HY000]: General error: 1005 Can#39;t create table - Laravel 4(SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4)

        1. <small id='0xYqe'></small><noframes id='0xYqe'>

              <tfoot id='0xYqe'></tfoot><legend id='0xYqe'><style id='0xYqe'><dir id='0xYqe'><q id='0xYqe'></q></dir></style></legend>
                  <tbody id='0xYqe'></tbody>
                <i id='0xYqe'><tr id='0xYqe'><dt id='0xYqe'><q id='0xYqe'><span id='0xYqe'><b id='0xYqe'><form id='0xYqe'><ins id='0xYqe'></ins><ul id='0xYqe'></ul><sub id='0xYqe'></sub></form><legend id='0xYqe'></legend><bdo id='0xYqe'><pre id='0xYqe'><center id='0xYqe'></center></pre></bdo></b><th id='0xYqe'></th></span></q></dt></tr></i><div id='0xYqe'><tfoot id='0xYqe'></tfoot><dl id='0xYqe'><fieldset id='0xYqe'></fieldset></dl></div>
                  <bdo id='0xYqe'></bdo><ul id='0xYqe'></ul>
                • 本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Laravel 4 - Connect to other database(Laravel 4 - 连接到其他数据库)
                  Call external API function from controller, LARAVEL 4(从控制器调用外部 API 函数,LARAVEL 4)
                  Empty string instead of null values Eloquent(空字符串而不是空值 Eloquent)
                  quot;laravel.logquot; could not be opened: failed to open stream(“laravel.log无法打开:无法打开流)
                  Displaying the Error Messages in Laravel after being Redirected from controller(从控制器重定向后在 Laravel 中显示错误消息)
                  Laravel Creating Dynamic Routes to controllers from Mysql database(Laravel 从 Mysql 数据库创建到控制器的动态路由)
                    <bdo id='5udf0'></bdo><ul id='5udf0'></ul>
                        <tbody id='5udf0'></tbody>
                          1. <small id='5udf0'></small><noframes id='5udf0'>

                            <tfoot id='5udf0'></tfoot>
                            <i id='5udf0'><tr id='5udf0'><dt id='5udf0'><q id='5udf0'><span id='5udf0'><b id='5udf0'><form id='5udf0'><ins id='5udf0'></ins><ul id='5udf0'></ul><sub id='5udf0'></sub></form><legend id='5udf0'></legend><bdo id='5udf0'><pre id='5udf0'><center id='5udf0'></center></pre></bdo></b><th id='5udf0'></th></span></q></dt></tr></i><div id='5udf0'><tfoot id='5udf0'></tfoot><dl id='5udf0'><fieldset id='5udf0'></fieldset></dl></div>

                            <legend id='5udf0'><style id='5udf0'><dir id='5udf0'><q id='5udf0'></q></dir></style></legend>