Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我可以写什么来修复ActiveRecord::RecordNotUnique?_Ruby On Rails_Activerecord_Rails Activerecord - Fatal编程技术网

Ruby on rails 我可以写什么来修复ActiveRecord::RecordNotUnique?

Ruby on rails 我可以写什么来修复ActiveRecord::RecordNotUnique?,ruby-on-rails,activerecord,rails-activerecord,Ruby On Rails,Activerecord,Rails Activerecord,我似乎无法摆脱这个例外。我有以下代码: class Batch < ApplicationRecord before_create :upcase_session_id def self.for_session(session_id, opts) batch = Batch.where(session_id: session_id.upcase).first_or_initialize if batch.new_record? batch.

我似乎无法摆脱这个例外。我有以下代码:

class Batch < ApplicationRecord

  before_create :upcase_session_id     

  def self.for_session(session_id, opts)
    batch = Batch.where(session_id: session_id.upcase).first_or_initialize
    if batch.new_record?
      batch.property = opts[:property]
      batch.save!
    end
    batch
  end

  private

  def upcase_session_id
    self.session_id = session_id.upcase
  end
end
架构批处理表:

 create_table "batches", force: :cascade do |t|
    t.string   "session_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "property_id"
    t.index ["property_id"], name: "index_batches_on_property_id", using: :btree
    t.index ["session_id"], name: "index_batches_on_session_id", unique: true, using: :btree
  end
我不确定如何更改此代码以停止获取我不断获取的此异常

例外情况:

PG::UniqueViolation: ERROR: duplicate key value violates unique constraint 
"index_batches_on_session_id" DETAIL: Key (session_id)=(A19A5BFD-A90C-40B4-9384-5C6C1C88213B) already exists. : 
INSERT INTO "batches" ("session_id", "created_at", "updated_at", "property_id") 
VALUES ($1, $2, $3, $4) RETURNING "id" where my schema.rb shows:
t.index ["session_id"], name: "index_batches_on_session_id", unique: true, using: :btree 

当记录因违反唯一性约束而无法插入时,将引发异常。您应该告诉我们表约束,因为问题可能是任何列

你可以试试这个:

class Batch < ApplicationRecord

  def self.for_session(session_id, opts)
    Batch.where(session_id: session_id.upcase).first_or_create(property: opts[:property])
  end

end
类批处理
您能提供您的
批处理
型号的代码吗?提供的信息还不够,但我可以假设:1:)session\u id在保存前设置为donwcase,因此在搜索行中,您可以通过
session\u id.upcase
搜索始终返回新记录,在save line
批处理中,session\u id
保存为downcase,因此,提出了例外情况。2:)
属性
有一些唯一的验证,需要在
保存之前进行检查@MikhailKatrin我更新了我的型号代码,上面有什么异常?您不需要
保存
创建之前
操作请参见帖子标题我仍然会收到此错误代码:PG::UniqueViolation:错误:重复键值违反唯一约束“会话id上的索引批次”详细信息:键(会话id)=(A19A5BFD-A90C-40B4-9384-5C6C1C88213B)已存在:将值($1,$2,$3,$4)插入“批处理”(“会话\u id”、“创建的\u at”、“更新的\u at”、“属性\u id”)中,返回“id”,其中my schema.rb显示:t.index[“会话\u id”],名称:“会话\u id上的索引\u批处理”,唯一:true,使用::b是否存在索引损坏的可能性?您可以尝试重新索引,看看它是否解决了问题。看起来REINDEX并没有解决问题:(能否显示数据库定义(除了迁移之外,数据库中的真实类型)?
class Batch < ApplicationRecord

  def self.for_session(session_id, opts)
    Batch.where(session_id: session_id.upcase).first_or_create(property: opts[:property])
  end

end