Mysql Laravel:如何在创建模式后通过phpmyadmin导入sql文件而不引发约束异常?
嗨,我有以下模式代码:Mysql Laravel:如何在创建模式后通过phpmyadmin导入sql文件而不引发约束异常?,mysql,foreign-keys,laravel-5,Mysql,Foreign Keys,Laravel 5,嗨,我有以下模式代码: public function up() { Schema::create('states', function(Blueprint $table) { $table->increments('id'); $table->string('acronym'); $table->string('name'); }); Schema::create('cities', funct
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
,约束cities\u state\u id\u foreign
外键(state\u id
)引用states
(id
)在进行迁移之前,它工作得很好,因此我想知道是否有一种方法可以在不删除外键的情况下导入文件
提前感谢。您的迁移很好,问题出在您的数据中 由于您已添加外键约束,因此在导入城市之前,必须存在具有此id的状态 现在您的状态id自动递增,所以您需要覆盖它,或者绝对确保它从insert order获得正确的id