Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uppercase UUIDs cause incorrect attribute dirtying #58

Open
jas14 opened this issue May 25, 2023 · 0 comments · May be fixed by #59
Open

Uppercase UUIDs cause incorrect attribute dirtying #58

jas14 opened this issue May 25, 2023 · 0 comments · May be fixed by #59

Comments

@jas14
Copy link

jas14 commented May 25, 2023

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 jas14 linked a pull request May 25, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant