Skip to content

Conversation

tylerwillingham
Copy link
Contributor

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

  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 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
Copy link
Contributor Author

A question I'm anticipating and open to solving is how do actually find the associated record?

I'm not sure how much we want to couple the solution inside of globalid to ActiveRecord. As-is, this would work in one of my applications because we use the friendly_id gem. I think this is easily achievable with where and find_by! but I don't know how agnostic from ActiveRecord this gem is intended to be

@arjun810
Copy link

We're also interested in using unsigned GIDs with nonsequential IDs, so this would have been helpful for us

@rafaelfranca
Copy link
Member

I'm not sure this configuration makes much sense since the locator part isn't solved. To make this work we would have to make sure the locator works with a configuration. If this change still require application developers to implement they own locator, so it would be better for you to override to_global_id in your model to call GlobalID.build instead of GlobalID.create

@tylerwillingham
Copy link
Contributor Author

tylerwillingham commented Sep 17, 2025

@rafaelfranca I stopped short of implementing the locator because in the app at work we've been using friendly_id and this would get us far enough, but I agree with solving the locator in order to upstream this into Rails.

I'm going to close this PR for now since it's not complete as-is, but I'll circle back and re-open it with a proposal that solve locating too.

@tylerwillingham
Copy link
Contributor Author

@rafaelfranca I opened a new PR - #199 - including a locator. I shouldn't have closed this PR but I thought it was going to take me longer to get around to.

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.

3 participants