简体   繁体   English

保存在Neo4j数据库中之前如何使用Object-Graph-Mapping在py2neo对象上创建标签

[英]How to create label on py2neo object using Object-Graph-Mapping before saved in Neo4j database

I am trying to save a node with a label but i can't. 我正在尝试保存带有标签的节点,但是我不能。 Only node with properties will save on neo4j db. 仅具有属性的节点将保存在neo4j db上。 I appreciate if anyone can help with how to create a label on an object before i save it. 如果有人能在保存之前在对象上创建标签,我将不胜感激。 I am using python and django with py2neo object-graph mapping for neo4j db. 我正在将python和django与py2neo对象图映射用于neo4j db。 Here is the code. 这是代码。 (In cypher this could be achieved using CREATE (n:Person{ id : id#, displayName : 'My Name' }) but i want to use py2neo object-graph mapping.) (在密码中,可以使用CREATE (n:Person{ id : id#, displayName : 'My Name' })但我想使用py2neo对象图映射。)

In model.py I have model.py我有

class Person(object):

    def __init__(self, id=None, displayName=None):
        self.id = id
        self.displayName = displayName

    def __str__(self):
        return self.displayName

in another .py file i have 在另一个.py文件中

from py2neo import neo4j
from py2neo import ogm
graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")`

def addPeople():
    store = ogm.Store(graph_db)
    worker = model.Person(1, "My Name")
    store.save_unique("People","ID",worker.id,worker)`

Here the node will be created with id and dispalyName property, but not with label. 在这里,将使用iddispalyName属性创建该节点,但不使用label创建该节点。

The OGM module was initially designed before the label/schema functionality was added to Neo4j. OGM模块最初是在将标签/模式功能添加到Neo4j之前设计的。 Therefore it is unaware of labels and schema indexes in general and uses legacy indexing instead. 因此,它通常不了解标签和架构索引,而改用传统索引。 Make sure you are familiar with the differences between these two types of index: 确保您熟悉以下两种类型的索引之间的区别:

The REST API interface does not easily lend itself to creating nodes with label detail in one request so this is not an easily change to make. REST API接口很难轻易地在一个请求中创建带有标签详细信息的节点,因此这并非易事。 I may rework the OGM module in future to support labels and schema indexes instead but for your application, you probably want to look at Cypher instead. 将来,我可能会重新设计OGM模块以支持标签和架构索引,但是对于您的应用程序,您可能希望改为使用Cypher。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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