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

Incompatible with UUID primary keys #30

Open
LeoAdamek opened this issue Feb 18, 2020 · 0 comments
Open

Incompatible with UUID primary keys #30

LeoAdamek opened this issue Feb 18, 2020 · 0 comments

Comments

@LeoAdamek
Copy link

Currently this gem is incompatible with UUID primary keys. This is because postgres does not allow hyphens in lpath values.

I've tried overriding ltree_fragment_column to try and sanitize the UUID value to something postgres will accept:

class Item < ActiveRecord::Base
  has_ltree_hierarchy ltree_fragment_column: :ltree_fragment

  def ltree_fragment
    primary_key.gsub('-', '_')
  end
end

This resolves the issue insofar as the values entered into the path column are now valid (e.g. 00ed4884_4c16_4ea9_97b0_0354fd5877c2 instead of 00ed4884-4c16-4ea9-97b0-0354fd5877c2)

However it then isn't possible to retrieve the record as it doesn't then know what this value relates to.

I'm wondering if special handling is required for UUID primary keys to deal with this?

Ought to be possible in the has_ltree_hierarchy method to check the type of the primary key in base.columns
e.g. is_uuid = base.columns.first.sql_type_metadata.type == :uuid

Then automatically translate the values to be ltree-safe on input, and reverse the operation on output.

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

No branches or pull requests

1 participant