dream752614590
2017-10-07 06:41
浏览 618
已采纳

Laravel事务回滚似乎不起作用

I am running laravel 5.4 and noticed that rollbacks in transactions do not work. I set my database engine to InnoDB in the settings.php file and tried DB::rollback(); and DB::rollBack(); (i.e. upper and lower case b) but it does not roll back my database.

I wrote a unit test bellow. It creates a record, commits it, then rolls back. However, the last assertion fails. After it rolls back, the record is still found in the database. Is there something I am missing? Or is there a bug with laravel?

public function testRollback()
{
    $this->artisan('migrate:refresh', [
        '--seed' => '1'
    ]);

    DB::beginTransaction();

    Season::create(['start_date' => Carbon::now(), 'end_date' => Carbon::now(),]);
    DB::commit();
    $this->assertDatabaseHas('seasons', [
        'start_date' => Carbon::now(), 'end_date' => Carbon::now(),
    ]);

    DB::rollBack();
    // This assertion fails. It still finds the record after calling roll back
    $this->assertDatabaseMissing('seasons', [
        'start_date' => Carbon::now(), 'end_date' => Carbon::now(),
    ]);
}

图片转代码服务由CSDN问答提供 功能建议

我正在运行 laravel 5.4 ,并注意到事务中的回滚不起作用。 我将我的数据库引擎设置为 settings.php 文件中的 InnoDB ,并尝试 DB :: rollback(); DB :: rollBack(); (即大写和小写b)但它不会回滚我的数据库。

我写了一个单元测试。 它创建一个记录,提交它,然后回滚。 但是,最后一个断言失败了。 回滚后,仍会在数据库中找到该记录。 有什么我想念的吗? 或者laravel有错误吗?

  public function testRollback()
 {
 $ this-> artisan('migrate:refresh',[
' -  -seed'=>'1'
]); 
 
 DB :: beginTransaction(); 
 
 Season :: create(['start_date'=> Carbon :: now(),'end_date  '=> Carbon :: now(),]); 
 DB :: commit(); 
 $ this-> assertDatabaseHas('seasons',[
'start_date'=> Carbon :: now(  ),'end_date'=> Carbon :: now(),
]); 
 
 DB :: rollBack(); 
 //此断言失败。 它在调用回滚后仍然找到记录
 $ this-> assertDatabaseMissing('seasons',[
'start_date'=> Carbon :: now(),'end_date'=> Carbon :: now()  ,
]); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题