Skip to content

Conversation

tylerwillingham
Copy link
Contributor

@tylerwillingham tylerwillingham commented Sep 17, 2025

With this proposal, GlobalID::Identification would implement global_id_column to set the desired column for GlobalID to reference on a per-model basis

i.e.

class Person
  attr_accessor :id, :external_id

  self.global_id_column :external_id
end

Person.new(id: "id-value", external_id: "external-id-value").to_gid.to_s
# => gid://app/Person/external-id-value

I think this is particularly useful for teams in the process of migrating off of auto-incremented primary keys, or are interested in using unsigned GIDs with some level of obscurity that's not attainable without starting to make a bigger change like actually implementing UUIDs as PKs

This PR extends beyond #185's initial implementation and add's support to the default locator to use these alternative columns for look-up

end
end

def global_id_method
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this method? We can just set the column to be :id by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I updated the implementation to use the class_attribute extension from ActiveSupport and brought consistency to the naming

Comment on lines +165 to +169
if model_class.global_id_column == :id
model_class.find gid.model_id
else
model_class.find_by model_class.global_id_column => gid.model_id
end
Copy link
Contributor Author

@tylerwillingham tylerwillingham Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💭 Simplifying on find_by (or find_by!) for ActiveRecord users feels nice, but I stopped short of doing that because I recall seeing that globalid doesn't want to make ORM assumptions

@tylerwillingham tylerwillingham force-pushed the twilling/alternative-primary-keys-plus-locator branch from 85e19f0 to 57d724b Compare September 18, 2025 17:51
This allows overriding the column used to define GIDs
Because of Identification.global_id_column, we can keep this internal
API much cleaner and avoid mucking with hash state to pass around this
column
`Model.primary_key` is actually irrelevant to this feature, the only
thing that matters is the model method.

For composite keys, or primary keys not named "id", we still get the
important value from `Model#id`, the only time we don't is when we need
to use a non-PK column.
@tylerwillingham tylerwillingham force-pushed the twilling/alternative-primary-keys-plus-locator branch from 57d724b to 13a4a69 Compare September 18, 2025 17:54
…tribute

This simplifies the implementation's API substantially by defining both
instance and class methods, and rails/globalid already depends on
ActiveSupport
@tylerwillingham tylerwillingham force-pushed the twilling/alternative-primary-keys-plus-locator branch from 13a4a69 to b551218 Compare September 19, 2025 01:26
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 this pull request may close these issues.

2 participants