错误:当前事务被中止,命令被忽略,直到事务块结束,Ruby on Rails

Mat*_*ski 4 ruby transactions ruby-on-rails

我的应用程序中有一辆模型车.我添加了色域.我的迁移看起来像这样:

class AddColorToCars < ActiveRecord::Migration
  def change
    add_column :cars, :color, :string
    Car.all.each do |car|
      car.color = "silver"
      car.save
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

以我的形式我添加:

= f.input :color
Run Code Online (Sandbox Code Playgroud)

在汽车模型中,我添加了验证:

validates :color, presence: true
Run Code Online (Sandbox Code Playgroud)

当我尝试编辑现有的Car并将其颜色更改为nil时出现以下错误:

ERROR: current transaction is aborted, commands ignored until end of transaction block
Run Code Online (Sandbox Code Playgroud)

当我禁用我的验证时,一切正常.怎么了?

Oss*_*Oss 10

这是所有与transactionsRailsPostgreSQL.

在某些数据库系统(如PostgreSQL)上,事务中的数据库错误会导致整个事务在从头开始重新启动之前变得不可用.

您有一个被捕获的事务中止.在ActiveRecord模型上添加验证时有时会导致这种情况.出于某种原因,一旦打破了INSERTPostgreSQL ,就无法在事务中的表上创建任何内容.此外,添加验证有时会破坏INSERT.

快速解决方案: 重新启动Rails服务器,您的验证将完美无缺.