Reputation: 11
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
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
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