makandra / active_type

Make any Ruby object quack like ActiveRecord
MIT License
1.09k stars 74 forks source link

Bug - Dupliate key error when casted object is saved #146

Closed KSH-code closed 2 years ago

KSH-code commented 3 years ago

From 1.8.0, with rails 6.1.4, the duplicate key error occurs when unsaved and casted object is saved.

e.g.

casted_object = ActiveType.cast(object.sub_objects.build, SubObject::Casting)
casted_object.save! # duplicate key error (Primary Key)

I'm investigating how to reproduce the problem using current repository.

Before complete investigating, to report, I created this issue.

p.s. I'm using MariaDB.

kratob commented 3 years ago

Hi. If you can give a minimal example to reproduce this error, we could take a look.

KSH-code commented 3 years ago

Oooops, sorry to too late reply.

table info

corners

displays

Our model code is

class Corner
  has_many :displays, inverse_of: :corner, foreign_key: :corner_id

  def change_status
    self.status = 10 # For simple
  end
end

class Display
  belongs_to :corner, inverse_of: :displays, foreign_key: :corner_id

  after_save :update_corner_status

  private

  def update_corner_status
    corner.change_status
    corner.save!
  end
end

class DisplayForm < ActiveType::Record[Display] # for callbacks via submit form
end

code to raise error

corner = load_corner

display = ActiveType.cast(corner.displays.build, DisplayForm)
display.save!

If lack of code, please mention me!