mysql-binuuid-rails icon indicating copy to clipboard operation
mysql-binuuid-rails copied to clipboard

Uppercase UUIDs cause incorrect attribute dirtying

Open jas14 opened this issue 2 years ago • 0 comments

Reloading relations appears to reassign the in-memory parent record ID to the related records' in-memory foreign keys. Additionally, UUIDs are case-insensitive, but are always surfaced to/by ActiveRecord as lowercase.

Consequently, when uppercase UUIDs are supplied as foreign keys in a relation, and the relation is reloaded, ActiveRecord views the in-memory UUID foreign keys on the related records as different from their values in the database, even though UUIDs are just hex representations of binary data (which is inherently uncased).

Repro follows:

# Assuming CREATE TABLE `parents` (`id` BINARY(16) NOT NULL PRIMARY KEY);
class Parent < ActiveRecord::Base
  attribute :id, MySQLBinUUID::Type.new

  has_many :children
end

# Assuming CREATE TABLE `children` (`id` BINARY(16) NOT NULL PRIMARY KEY, `parent_id` BINARY(16) NOT NULL);
class Child < ActiveRecord::Base
  attribute :id, MySQLBinUUID::Type.new
  attribute :parent_id, MySQLBinUUID::Type.new

  belongs_to :parent
end

parent = Parent.create!(id: "DEADBEEF-0000-0000-0000-000000000000")
parent.children.create!(id: SecureRandom.uuid)
parent.children.first.parent_id # => "deadbeef-0000-0000-0000-000000000000"
parent.children.first.parent_id_changed? # => false

parent.children.reload

parent.children.first.parent_id # => "DEADBEEF-0000-0000-0000-000000000000"
parent.children.first.parent_id_changed? # => true
parent.children.first.parent_id_change # => ["deadbeef-0000-0000-0000-000000000000", "DEADBEEF-0000-0000-0000-000000000000"]

jas14 avatar May 25 '23 16:05 jas14