How to override ruby neo4j node primary id

239 Views Asked by At

I have the following model:

class User
  include Neo4j::ActiveNode
end

and I simply want to specify the primary key at creation time:

user = User.create(id: SecureRandom.uuid)

However, this does not work and return Undefined properties: id. I have tried with a custom id_property definition:

class User
  include Neo4j::ActiveNode
  id_property :user_id
end

While this works, it won't provide a default id when doing User.create. So I tried:

class User
  include Neo4j::ActiveNode
  id_property :user_id, auto: :uuid
end

which will assign a default uuid but does not allow User.create(user_id: xxx) anymore!

This is driving me crazy! Am I missing something here?

1

There are 1 best solutions below

3
Brian Underwood On

This is a long-standing issue with the gem, that you can't set your own IDs on nodes.

Is the SecureRandom.uuid just an example? Because that's exactly what's used for the :uuid strategy (which is the default for the id property). I'm not sure what you're trying to achieve.

The GraphAware UUID plugin is great, though I don't know how it will work with the gem which wants to generate it's own IDs. You might be able to create your own UUID generator which just generates nil values and the let the plugin set the ID (at which point the gem doesn't care about the value, so long as it's present and unique). Unfortunately the gem can't depend on that plugin being installed in all Neo4j servers it's used on.

Hopefully someday in the near future Neo4j will support IDs which don't get recycled so that the gem doesn't need to have it's own UUID logic