globalid icon indicating copy to clipboard operation
globalid copied to clipboard

Add support for non-PK columns

Open tylerwillingham opened this issue 1 year ago • 1 comments

  • Implements #184

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

tylerwillingham avatar Aug 23 '24 22:08 tylerwillingham

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

tylerwillingham avatar Aug 23 '24 22:08 tylerwillingham

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

arjun810 avatar Sep 17 '25 07:09 arjun810

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

rafaelfranca avatar Sep 17 '25 20:09 rafaelfranca

@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 avatar Sep 17 '25 21:09 tylerwillingham

@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.

tylerwillingham avatar Sep 17 '25 22:09 tylerwillingham