简体   繁体   中英

Laravel 8.54 Migration and Seeding Cannot truncate a table referenced in a foreign key constraint

I'm trying to seed my UserSeeder into my database but I keep running into this error:

SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint ( hospitalmanagement . role_user , CONSTRAINT role_user_user_id_foreign FOREIGN KEY ( user_id ) REFERENCES hospitalmanagement . users ( id )) (SQL: truncate table users )

Here's my users table code

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->text('photo')->default('images/default-user-photo.jpg');
        $table->string('email')->unique();
        $table->integer('phone')->unique();
        $table->integer('status')->default(1);
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->string('slug')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}

My roles table

public function up()
{
    Schema::create('roles', function (Blueprint $table) {
        $table->id();
        $table->string('name')->unique();
        $table->string('slug')->unique();
        $table->timestamps();
    });
}

User role table

public function up()
{
    Schema::create('role_user', function (Blueprint $table) {
        $table->primary(['role_id', 'user_id']);
        $table->foreignId('role_id')->constrained()->onDelete('cascade');
        $table->foreignId('user_id')->constrained()->onDelete('cascade');
        $table->timestamps();
    });
}

In my seeders file I have this in my RoleSeeder

public function run()
{
    // Role::truncate();

    $role = new Role();
    $role->name = 'Super Admin';
    $role->slug = 'super-admin';

    $role = new Role();
    $role->name = 'Admin';
    $role->slug = 'admin';

    $role = new Role();
    $role->name = 'Doctor';
    $role->slug = 'doctor';
}

UserSeeder file

public function run()
{
    User::truncate();

    $user = new User();
    $user->name = 'Super Admin';
    $user->email = 'admin@gmail.com';
    $user->phone = '0278596525';
    $user->role_id = 1;
    $user->password = '$2y$10$wSoyqSUQZGC8MeEcbqu2gugCgGTOuJz5AiKCphM6W2rK8swfb2/ky';
    $user->created_at = Carbon::now()->toDateTimeString();
    $user->save();

    $user = new User();
    $user->name = 'Admin';
    $user->email = 'add@gmail.com';
    $user->phone = '0247859662';
    $user->role_id = 2;
    $user->password = '$2y$10$wSoyqSUQZGC8MeEcbqu2gugCgGTOuJz5AiKCphM6W2rK8swfb2/ky';
    $user->created_at = Carbon::now()->toDateTimeString();
    $user->save();

    $user = new User();
    $user->name = 'Doctor';
    $user->email = 'doc@gmail.com';
    $user->phone = '0247899663';
    $user->role_id = 3;
    $user->password = '$2a$12$CMMLYGNwNreJlghJY3O3b.U4k2QJ7fhXdAA4XtfRXTPEEtBZenYNq';
    $user->created_at = Carbon::now()->toDateTimeString();
    $user->save();
}

you can try like this:

 DB::statement('SET FOREIGN_KEY_CHECKS = 0'); //close foreign_key limit
 User::truncate()
 DB::statement('SET FOREIGN_KEY_CHECKS = 1'); //open foreign_key limit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM