Skip to content

Uppercase UUIDs cause incorrect attribute dirtying #58

@jas14

Description

@jas14

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"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions