无法将GraphObject强制转换为Node

时间:2016-09-19 08:51:14

标签: neo4j py2neo

我定义<a>作为AbstractQuery的孩子:

GraphObject

并迭代我(已经存在的)图形中的所有class AbstractQuery(GraphObject): __primarykey__ = "hash" hash = Property() projname = Property() def __init__(self, hash): self.hash = hash self.projname = "" # TODO:initialize this 个对象,并希望创建一个由哈希组成的新SQLQuery。应该连接确定AbstractQuery的哈希值的任何SQLQuery哈希:

AbstractQuery

在开始此过程之前,我提取所有已存在的AbstractQueries的表(使用哈希作为键)以防止创建相同的AbstractQuery两次。

但是,当我运行该方法时,我最终会遇到异常:

  

TypeError:无法将AbstractQuery强制转换为节点

为什么会这样,我该如何解决?

我之前使用此表示法在我的图表中输入了多个def addAbstractionLayerSqlQueries(graph, logger=None): abstractQueryTable = getAbstractQueries(graph, logger) sqlqueries = graph.data("MATCH (n:SQLQuery) return n") if logger is not None: logger.info("abstracting {} queries".format(len(sqlqueries))) counter = 1 for iterator in sqlqueries: query = iterator['n'] if logger is not None: logger.info("abstracting query {}/{}".format(counter, len(sqlqueries))) hash = sqlNormalizer.generate_normalized_query_hash(query['ts']) if hash not in abstractQueryTable: abstractQueryNode = al.AbstractQuery(hash) abstractQueryTable[hash] = abstractQueryNode graph.push(abstractQueryNode) rel = Relationship(query, "ABSTRACTSTO", abstractQueryTable[hash]) graph.create(rel) counter = counter + 1

SQLQueries

由于我能够使用此表示来表示和输入节点,因此我非常惊讶于为什么py2neo拒绝我的class SQLQuery(GraphObject): __primarykey__ = "uuid" uuid = Property() projname = Property() session = Property() user = Property() seq = Property() ts = Property() sql = Property() Statement = RelatedTo("SQLStatement") AbstractsTo = RelatedTo("AbstractQuery") def __init__(self, projname=None, session=None, user=None, seq=None, ts=None, sql=None): self.projname = projname self.session = session self.user = user self.seq = seq self.ts = ts self.sql = sql self.uuid = "{} [{} {}] {}.{}.{}".format(type(self).__name__, seq, ts, projname, session, user) 类作为AbstractQuery函数中的节点。

2 个答案:

答案 0 :(得分:2)

你正在混合两层API。 OGM层位于常规py2neo API之上,GraphObject不直接对应Node(它也包含其他内容)。因此,您无法直接与Relationship建立GraphObject

要访问GraphObject后面的核心节点,您可以使用my_object.__ogm__.node

答案 1 :(得分:0)

我通过仅使用对象模型并替换graph.data(...)查询部分来解决问题:

def addAbstractionLayerSqlQueries(graph, logger=None):
    abstractQueryTable = getAbstractQueries(graph, logger)
    sqlqueries = list(adlsql.SQLQuery.select(graph))

    print type(sqlqueries)

    if logger is not None:
        logger.info("abstracting {} queries".format(len(sqlqueries)))

    counter = 1
    for query in sqlqueries:
        print type(query)

        if logger is not None:
            logger.info("abstracting query {}/{}".format(counter,
                                                         len(sqlqueries)))

        hash = sqlNormalizer.generate_normalized_query_hash(query.ts)

        if hash not in abstractQueryTable:
            abstractQueryNode = al.AbstractQuery(hash)
            abstractQueryTable[hash] = abstractQueryNode
            graph.push(abstractQueryNode)

        query.AbstractsTo.add(abstractQueryTable[hash])
        graph.push(query)
        counter = counter + 1

但是,错误的实际原因仍然未知,我会接受解释此问题的任何答案。这个答案只能解决问题。