【问题标题】:Access Violation error in Laravel-4 migrationLaravel-4 迁移中的访问冲突错误
【发布时间】:2014-01-07 14:14:42
【问题描述】:

我的迁移如下:

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

在添加给我以下错误的外键约束之后:

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`)

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

【问题讨论】:

  • 添加外键后有什么变化?
  • 我刚刚在预测表$table->foreign('user_id')->references('id')->on('users');$table->foreign('fixture_id')->references('id')->on('fixtures');中添加了外键约束
  • 好的.. 您可以尝试按此顺序迁移吗?用户、灯具,然后是预测……看看它是否有效。 :)
  • 即使这样工作,我也不认为它是一个合适的解决方案。将来如果我有另外 20 个表,一次迁移一个表是不合适的,这也是为了在引用键表之后创建带有 FK 的表。
  • 回滚 Laravel 迁移并不神奇。它只是调用迁移文件中的down() 函数。因此,down() 函数中的操作中断的任何规则都将导致迁移回滚问题。在这种情况下,MySQL 无法删除该表,因为外键关系阻止了它。

标签: php mysql laravel migration laravel-4


【解决方案1】:

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

但是,该表不存在!

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

【讨论】:

    【解决方案2】:

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

    你应该很高兴再次迁移。

    加法

    注意down()函数中表的删除顺序。

    删除一些具有onDelete() 声明的引用约束的表肯定需要先删除约束。

    我认为这会导致您的回滚错误。

    更多地了解您的迁移,伙计 :-)

    【讨论】:

      猜你喜欢
      • 2018-06-28
      • 1970-01-01
      • 2021-02-08
      • 2018-02-06
      • 2023-02-10
      • 2014-06-10
      • 2018-06-01
      • 2023-02-03
      • 2017-11-16
      相关资源
      最近更新 更多