我有以下移民:
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`)我哪里出问题了?我对迁移很陌生,所以这些可能是初学者通常会犯的错误。但我不明白我到底做错了什么。我希望有人能帮我。
发布于 2015-01-08 03:13:57
移徙将继续下去,直到遇到一些问题。因此,可能已经添加了外键,但由于错误,它的关联表尚未创建。如果您试图回滚,迁移将假定存在与外键关联的表,并且它也将尝试回滚该表。
但是,该表不存在!
最简单的方法是清空数据库并重新运行迁移。我以前遇到过这个问题,一旦出现这样的问题就很难一步一步地回滚。
https://stackoverflow.com/questions/20674446
复制相似问题