stefankroes / ancestry

Organise ActiveRecord model into a tree structure
MIT License
3.71k stars 458 forks source link

Configuring Ancestry to use UUID keys #644

Closed sheminanto closed 1 year ago

sheminanto commented 1 year ago

While adding the configuration Ancestry.default_ancestry_format = :materialized_path2 in config/initializers/ancestry.rb, not able to create a new child node. The errorAncestry is invalid (ActiveRecord::RecordInvalid) is thrown when trying to create a new child node. But, It's possible to create a root node. The error is thrown only while creating a new node with a parent.

current model config:

class [Model] < ApplicationRecord
  has_ancestry orphan_strategy: :adopt, primary_key_format: /\A[\w\-]+(\/[\w\-]+)*\z/
end

Rails version: 7.0.4 Ruby version: 3.1.3 OS: macOS Ventura 13.1 Database: PostgreSQL 14.7 Primary key type: UUID ancestry gem version: 4.3.1

kbrock commented 1 year ago

Hello @sheminanto

I'm sorry to hear this is giving you problems.

Since the root node has a path of '/', it is the child node that is going to give you problems if the primary_key_format is not correct.

  1. Is this a new installation, or are you migrating from :matrialized_path?
  2. Have you tried passing in the format for a single key?
# app/models/model.rb
class [Model] < ApplicationRecord
  has_ancestry orphan_strategy: :adopt, primary_key_format: '[\w\-]+'
end

Also, if all of you ancestry tables use uuid, you may want to consider:

# config/initializer/ancestry.rb
Ancestry.default_ancestry_format = :materialized_path2
Ancestry.default_primary_key_format = '[\w\-]+'

Also, I am curious if a primary_key_format of '[-A-Fa-f0-9]{36}' makes sense, or if I messed up in the docs and need another style. I have not used uuids for quite a long while.

sheminanto commented 1 year ago

@kbrock : Thanks for the immediate response. I am currently migrating from :materilalized_path. Both the formats '[\w\-]+' and '[-A-Fa-f0-9]{36}' are working with uuid. I think the wiki regarding the usage with UUID needs an update then.

kbrock commented 1 year ago

@sheminanto I updated the wiki.

Do you have any other questions or can we close this issue?

sheminanto commented 1 year ago

No other questions. I am closing this issue. Thank you @kbrock