TK0
TK0

Reputation: 11

There is no active transaction'

having problems with this function wipe_data

this wipe_data function my DB cleanup and admin data inserting but this function shows error:

There is no active transaction

This is my code:

 function wipe_data() {
     DB::beginTransaction();
     $adminData = User::where('role', 'admin')->first();  
    try {
        User::truncate();
        User_details::truncate();
        User_kyc::truncate();
        Token::truncate();`enter code here`
        $auto_id = date('Y');
        DB::statement("ALTER TABLE ls_users AUTO_INCREMENT = $auto_id");
        $admin = new User();
        $admin->username = $adminData->username;
        $admin->email = $adminData->email;
        $admin->password = $adminData->password;
        $admin->role = $adminData->role;
        $admin->save();
        $user_id = User::where('role', 'admin')->value('id');
        DB::commit();
    } catch (\Exception $ex) {
        DB::rollback();
        return false;
    }
    return true;
}

Upvotes: 1

Views: 2112

Answers (2)

gsl
gsl

Reputation: 86

truncate() is another statement with implicit commit(). It used to work inside a transaction, but after migrating to Amazon RDS for MySQL - AWS MySQL 8, the script started throwing this error.

Upvotes: 0

Raptor
Raptor

Reputation: 54212

There are some statements that caused implicit commit, including the ALTER TABLE statement you are using.

Therefore, your statement has already been committed before you called DB::commit(), hence an error occurred.

Upvotes: 7

Related Questions