Neoism与关系不存在相关联。

huangapple go评论127阅读模式
英文:

Neoism relate if relationship doesn't exist

问题

我刚开始使用neoism,并且到目前为止很喜欢它。我遇到了一个问题,想知道是我对neoism的不熟悉还是neoism本身的问题。

我在我的Go代码中有一行:

agent.Relate(relation, node.Id() , neoism.Props{})

问题是,如果我运行它超过一次,它会复制关系。是否有一种方法只在关系不存在时才创建 - 类似于GetOrCreateNodeFunction。

或者我需要编写一些原始的CQL代码,在运行上述语句之前检查关系是否已经存在吗?

提前感谢。

英文:

I've only just started using neoism and enjoying it so far. I've hit a bit of a problem and wondered if it my naivety of neoism or neoism itself that's at fault.

I've got a line in my go code:

agent.Relate(relation, node.Id() , neoism.Props{})

The issue is that if I run it more than once it will duplicate the relationship. Is there a way to create only if the relationship doesn't already exist - something similar to the GetOrCreateNodeFunction.

Or will I have to write some raw cql to check if the relationship already exists before running the statement above?

Thanks in advance

答案1

得分: 1

没有原生的函数或REST端点可以创建唯一的有向关系。您可以为每个关系分配一个唯一的属性值,并在关系属性上添加一个唯一索引,或者您可以使用Cypher查询和CREATE UNIQUE子句。

http://neo4j.com/docs/stable/query-create-unique.html#_create_unique_relationships

英文:

There is not a native function or REST endpoint for creating unique directed relationships. You might assign a unique property value to each relationship and add a unique index on the relationship property, or you might use a cypher query and the CREATE UNIQUE clause.

http://neo4j.com/docs/stable/query-create-unique.html#_create_unique_relationships

答案2

得分: 1

你可以使用以下函数,这是我在我的代码中使用的。它在以下位置有一个外部依赖:

github.com/imdario/mergo

以下通用函数适用于任何类型的节点和关系。

func GetOrCreateRelationship(from *neoism.Node, to *neoism.Node, relType string, props neoism.Props) (relationship *neoism.Relationship) {
    relationships, err := from.Relationships(relType)

    if err == nil {
        for _, relationship := range relationships {
            endNode, err := relationship.End()

            if err != nil {
                continue
            }

            if endNode.Id() == to.Id() {
                newProps, err := relationship.Properties()

                if err != nil {
                    return relationship
                }

                if err := mergo.Merge(&newProps, props); err != nil {
                    relationship.SetProperties(newProps)
                }

                return relationship
            }
        }
    }

    relationship, err = from.Relate(relType, to.Id(), props)

    if err != nil {
        log.Printf("Cannot create relationship: %s", err)
    }

    return
}
英文:

You can use the following function which I am using for my code. It has an external dependency at

> github.com/imdario/mergo

And the following generic function will work for any kind of node and relationships.

 func GetOrCreateRelationship(from *neoism.Node, to *neoism.Node, relType string, props neoism.Props) (relationship *neoism.Relationship) {
relationships, err := from.Relationships(relType)

if err == nil {
	for _, relationship := range relationships {
		endNode, err := relationship.End()

		if err != nil {
			continue
		}

		if endNode.Id() == to.Id() {
			newProps, err := relationship.Properties()

			if err != nil {
				return relationship
			}

			if err := mergo.Merge(&newProps, props); err != nil {
				relationship.SetProperties(newProps)
			}

			return relationship
		}
	}
}

relationship, err = from.Relate(relType, to.Id(), props)

if err != nil {
	log.Printf("Cannot create relationship: %s", err)
}

return
}

huangapple
  • 本文由 发表于 2016年3月23日 23:13:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/36181929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定