事务在截断时不起作用 [英] transaction doesn't work on truncate
问题描述
我第一次尝试 Laravel 事务......我使用 Eloquent 进行大部分查询,由于那里没有事务,我必须混合使用 Eloquent 和查询构建器.
I'm trying Laravel transaction for the first time... I do most of my queries with Eloquent and since there no transaction there I have to do a mixture of Eloquent and query builder.
这是我的代码:
DB::beginTransaction();
try{
Setting::truncate();
Setting::insert($data);
DB::commit();
jok('all ok');
}
catch (\Exception $e)
{
DB::rollback();
jerror('some error accorded! ');
}
所以我绑定了向设置添加一些无效数据,我得到了一些错误同意
错误,但在 INSERT 之前的查询 Setting::truncate();
无论如何都被执行了,我最终得到了一张空桌子.
So I've tied to add some invalid data to Settings and i got the some error accorded
error as expected but the query before INSERT Setting::truncate();
was executed anyway and I ended up with a empty table.
所以要么我做错了什么,要么事务在截断时不起作用.
So either I'm doing something wrong or transaction doesn't work on truncate.
推荐答案
数据库事务中的 TRUNCATE TABLE 将导致隐式 COMMIT 操作.DELETE 没有相同的行为,因此您可以安全地运行
TRUNCATE TABLE in a database transaction will cause a implicit COMMIT action. DELETE does not have that same behavior, so you can safely run
DELETE FROM tblname
这将清除您的表并仍然保留所需的事务回滚功能.
This will clear your table and still keep the desired rollback feature of a transaction.
请参阅有关 TRUNCATE 的 MySQL 文档.要点解释了这个功能 http://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
See the MySQL documentation on TRUNCATE. The bullet points explain this functionality http://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
这篇关于事务在截断时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!