简体   繁体   中英

Combining Neo4j with PostgreSQL under Rails application

We're building a Rails 3 app that supposed to use both Neo4j database and standard postgresql .

What is the best way of combining those to work together? We need them both to be ORM compatible and they need to behave well interacting each other (relationships with each other etc).

Thanks

Use ActiveRecord for Postgresql as you would normally. Add methods to the models that make calls to Neo4j and return IDs.

For a simple example:

A User has Tags . The User model would have a " tags " method. It would call a cypher query or rest query that brought back IDs ([2, 3, 5]) of tags from the graph database. Then I would load the proper tag object from postgres with a

Tag.find_all_by_id([2, 3, 5])

I think neoid provides the methods Max refers to in his answer .

From the GitHub repo:

Make your ActiveRecords stored and searchable on Neo4j graph database, in order to make fast graph queries that MySQL would crawl while doing them.

Neoid to Neo4j is like Sunspot to Solr. You get the benefits of Neo4j speed while keeping your schema on your plain old RDBMS.

Your application will need to co-ordinate activity between the databases.

It can be valuable to use multiple DBs where you need their different features badly enough, but there's a big complexity cost to this, and you lose basic features like foreign keys.

From the PostgreSQL side you can theoretically implement one side of foreign key relationships, preventing the deletion of records from within PostgreSQL when the remote side still exists, and preventing the insertion of records in PostgreSQL where the remote side does not exist. You would need to see if neo4j has similarly flexible user-defined triggers.

Even if it's possible, performance of foreign key enforcement is likely to be absolutely horrible.

To me, it does not make sense to choose to use multiple databases via a common abstraction layer. The main reason you would choose simultaneous use of multiple DBs is to benefit from the different features of each, and abstraction layers like ORMs just tend to get in the way of that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM