播种前无法截断 Table
Can't Truncate Table Befor Seeding
我想在 seed.i 之前 运行 对我的用户 table 进行分类 :
DatabaseSeeder.php :
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
public function run()
{
App\User::truncate();
factory(App\User::class,1)->create();
}
}
然后运行php artisan db:seed
出现错误:
In Connection.php line 664:
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constra
int (`mr_musicer`.`dislikes`, CONSTRAINT `dislikes_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `mr_musicer`
.`users` (`id`)) (SQL: truncate `users`)
In Connection.php line 458:
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constra
int (`mr_musicer`.`dislikes`, CONSTRAINT `dislikes_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `mr_musicer`
.`users` (`id`))
我现在想知道为什么我不能运行满足我的用户table!
您在其他 table 上提到了您的用户。您应该将 ->onDelete('cascade')
添加到引用用户 ID 的不喜欢 table 列,或者先手动删除所有不喜欢。
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
存在外键问题,table正在尝试提醒您。
如果你想截断 table 无论如何。
Schema::disableForeignKeyConstraints();
// ... Some Truncate Query
Schema::enableForeignKeyConstraints();
别忘了使用:
use Illuminate\Support\Facades\Schema;
当您在 table
中有外键时,在播种前对数据进行 运行 分类的最简单方法
只需在 运行 方法的开头添加这一行。
Schema::enableForeignKeyConstraints();
DB::truncate('posts');
Schema::disableForeignKeyConstraints();
我想在 seed.i 之前 运行 对我的用户 table 进行分类 :
DatabaseSeeder.php :
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
public function run()
{
App\User::truncate();
factory(App\User::class,1)->create();
}
}
然后运行php artisan db:seed
出现错误:
In Connection.php line 664:
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constra
int (`mr_musicer`.`dislikes`, CONSTRAINT `dislikes_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `mr_musicer`
.`users` (`id`)) (SQL: truncate `users`)
In Connection.php line 458:
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constra
int (`mr_musicer`.`dislikes`, CONSTRAINT `dislikes_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `mr_musicer`
.`users` (`id`))
我现在想知道为什么我不能运行满足我的用户table!
您在其他 table 上提到了您的用户。您应该将 ->onDelete('cascade')
添加到引用用户 ID 的不喜欢 table 列,或者先手动删除所有不喜欢。
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
存在外键问题,table正在尝试提醒您。 如果你想截断 table 无论如何。
Schema::disableForeignKeyConstraints();
// ... Some Truncate Query
Schema::enableForeignKeyConstraints();
别忘了使用:
use Illuminate\Support\Facades\Schema;
当您在 table
中有外键时,在播种前对数据进行 运行 分类的最简单方法只需在 运行 方法的开头添加这一行。
Schema::enableForeignKeyConstraints();
DB::truncate('posts');
Schema::disableForeignKeyConstraints();