2013-12-19 169 views
0

我有迁移如下:访问冲突错误

create_users_table.php

class CreateUsersTable extends Migration { 
    public function up() 
    { 
     Schema::create('users', function(Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name', 255); 
      $table->string('username', 64)->unique(); 
      $table->string('email', 255)->unique(); 
      $table->string('password',64); 
      $table->timestamps(); 
     }); 
    } 

    public function down() 
    { 
     Schema::drop('users'); 
    } 

} 

create_predictions_table.php

class CreatePredictionsTable extends Migration { 
    public function up() 
    { 
     Schema::create('predictions', function(Blueprint $table) { 
      $table->increments('id'); 
     $table->integer('user_id')->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users'); 
     $table->integer('fixture_id')->unsigned(); 
     $table->foreign('fixture_id')->references('id')->on('fixtures'); 
     $table->integer('home_team_score_prediction'); 
     $table->integer('away_team_score_prediction'); 
     $table->timestamps(); 
     }); 
    } 
    public function down() 
    { 
     Schema::drop('predictions'); 
    } 
} 

create_fixtures_table.php

class CreateFixturesTable extends Migration { 
    public function up() 
    { 
     Schema::create('fixtures', function(Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('home_team_id'); 
      $table->integer('away_team_id'); 
      $table->dateTime('date'); 
      $table->string('venue'); 
      $table->timestamps(); 
     }); 
    } 
    public function down() 
    { 
     Schema::drop('fixtures'); 
    } 
} 

起初,当我跑使用

php artisan migrate 

我没有加入外键的迁移。我跑

php artisan migrate:refresh 

加入这给了我以下errror外键约束后:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'predictions' already exists (SQL: create table `predictions` (`id` int unsigned not null auto_increment primary key, `user_id` int unsigned not null, `fixture_id` int 
    unsigned not null, `home_team_score_prediction` int not null, `away_team_score_prediction` int not null, `created_ at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci) 

我认为这是因为表已经在那里了(尽管刷新应该回滚迁移和运行它们再次)

当我试图使用回滚

php artisan migrate:rollback 

我得到以下:

[Illuminate\Database\QueryException] 
    SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constrain t fails (SQL: drop table `users`) 

我在哪里出错了?我是新来的移民,所以这些可能是初学者通常会遇到的错误。但我不明白我到底做错了什么。我希望有人能帮助我。

+0

当你添加外键时,你改变了什么? – Melvin

+0

我刚刚在预测表中添加了外键约束,这些表中的外键('user_id') - >引用('id') - > on('users');'和$ table-> foreign(' ('fix');' – md1hunox

+0

好吧..你可以尝试迁移这个序列吗?用户,灯具,然后预测...看看它是否工作。 :) – Melvin

回答

0

我做了什么 Laravel有一个migrations表。 Truncate或清空表格,然后删除与您的迁移相关的所有表格。 如果可能,请在删除表之前先删除对外键的任何约束。

你应该很好地再次迁移。

加成

保重表删除的down()功能的顺序。

删除onDelete()声明的带有参照约束的表格肯定需要首先删除约束。

我认为这是导致你的回滚错误。

看起来更进入你的迁移,好友:-)

1

迁移将继续下去,直到它遇到了一些问题。所以可能已经添加了一个外键,但是由于错误,其关联表还没有被创建。如果您尝试回滚,则迁移将假定与外键关联的表存在,并且它将尝试回滚该表。

但是,该表不存在!

那么,最简​​单的方法是清空数据库并重新运行迁移。我之前遇到过这个问题,一旦出现这样的问题,就很难一步步回滚。