Laravel:如何在创建模式后通过 phpmyadmin 导入 sql 文件而不抛出约束异常?
Laravel: how to import a sql file through phpmyadmin after you created a schema without throwing a constraint exception?
您好,我有以下架构代码:
public function up()
{
Schema::create('states', function(Blueprint $table)
{
$table->increments('id');
$table->string('acronym');
$table->string('name');
});
Schema::create('cities', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->integer('state_id')->unsigned();
$table->foreign('state_id')->references('id')->on('states');
});
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('fullname');
$table->string('photo');
$table->text('description');
$table->string('email')->unique();
$table->string('password', 60);
$table->integer('city_id')->unsigned();
$table->foreign('city_id')->references('id')->on('cities');
$table->rememberToken();
});
}
public function down()
{
Schema::drop('states');
Schema::drop('cities');
Schema::drop('users');
}
迁移工作正常,现在我想通过 phpmyadmin 导入一个 sql 文件,但我收到以下错误:
1452 - 无法添加或更新子行:外键约束失败 (yearbook
.cities
, CONSTRAINT cities_state_id_foreign
FOREIGN KEY (state_id
) REFERENCES states
( id
))
在进行迁移之前它运行良好,所以我想知道是否有一种方法可以在不删除外键的情况下导入文件。
提前致谢。
您的迁移没有问题,问题出在您的数据上。
由于您添加了外键约束,因此必须存在具有此 ID 的州才能导入城市。
现在您有状态 ID 自动递增,因此您需要覆盖它或绝对确保它从插入订单中获得正确的 ID。
您好,我有以下架构代码:
public function up()
{
Schema::create('states', function(Blueprint $table)
{
$table->increments('id');
$table->string('acronym');
$table->string('name');
});
Schema::create('cities', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->integer('state_id')->unsigned();
$table->foreign('state_id')->references('id')->on('states');
});
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('fullname');
$table->string('photo');
$table->text('description');
$table->string('email')->unique();
$table->string('password', 60);
$table->integer('city_id')->unsigned();
$table->foreign('city_id')->references('id')->on('cities');
$table->rememberToken();
});
}
public function down()
{
Schema::drop('states');
Schema::drop('cities');
Schema::drop('users');
}
迁移工作正常,现在我想通过 phpmyadmin 导入一个 sql 文件,但我收到以下错误:
1452 - 无法添加或更新子行:外键约束失败 (yearbook
.cities
, CONSTRAINT cities_state_id_foreign
FOREIGN KEY (state_id
) REFERENCES states
( id
))
在进行迁移之前它运行良好,所以我想知道是否有一种方法可以在不删除外键的情况下导入文件。
提前致谢。
您的迁移没有问题,问题出在您的数据上。
由于您添加了外键约束,因此必须存在具有此 ID 的州才能导入城市。
现在您有状态 ID 自动递增,因此您需要覆盖它或绝对确保它从插入订单中获得正确的 ID。