I make test in Laravel, and I want to truncate all users, and create only one. This code show me error SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint
. What can I do that truncate users ? Just I don’t want to use solutions that violate database settings, e.g.Schema::disableForeignKeyConstraints();
public function test_delete_user()
{
DB::table('users')->truncate();
$role = User::factory()->create();
.......
}
3
Answers
"I am currently facing the same issue"
Your table user have a forign key constraint remove that to get truncate working.
Can i see the database schema.
This is for MysQl try that on your table
Before using
You may try to use (suppose your foreign table name "addresses")
It may help you.
Then it may look like