Skip to content

Failed to reverse child/parent relation #118

Open
@lomoval

Description

@lomoval

Bug Report:

SaveDepth failed to save entity when you try to reverse relation with Link/Unlink function.
E.g. you have (node1)-[HAS_NODE]->(node2) and you wand to reverse relation to have (node1)<-[HAS_NODE]-(node2):

node2.UnlinkParent(node1)
node2.LinkChild(node1)
SaveDepth(node2) - error

Expected Behavior

After changing relation with Link/Unlink functions, SaveDepth function works without errors and relation is changed.

Current Behavior

SaveDepth gives failed to save in auto transaction, expected relationship deletions not equal to actual. Expected=2|Actual=1

Steps to Reproduce

Code example in the end.

  1. Use structure
type Node struct {
	gogm.BaseUUIDNode
	Name     string  `gogm:"name=name"`
	Children []*Node `gogm:"direction=outgoing;relationship=HAS_NODE"`
	Parents  []*Node `gogm:"direction=incoming;relationship=HAS_NODE"`
}
  1. Create functions with gogmcli
  2. Create 2 nodes
  3. Add node2 as child for node1
  4. Save node2 with depth 1
  5. Load node2
  6. Remove parent from node2 and add children:
node2.UnlinkFromNodeOnFieldParents(parent)
node2..LinkToNodeOnFieldChildren(parent)
  1. Save node2 with depth 1.

Possible Solution

Environment

Value
Go Version go version go1.18.3 windows/amd64
GoGM Version v2.3.6
Neo4J Version Version: 4.4.3 Edition: community
Operating System Win10 x64

Would you be interested in tackling this issue

No

Additional info

        n1 := &Node{Name: "n1"}
	n2 := &Node{Name: "n2"}
	n1.LinkToNodeOnFieldChildren(n2)

	err = sess.SaveDepth(ctx, n1, 1)
	if err != nil {
		log.Err(err).Msg("")
		return
	}

	n22 := &Node{}
	err = sess.LoadDepth(ctx, n22, n2.UUID, 1)
	if err != nil {
		log.Err(err).Msg("")
		return
	}
	fmt.Printf("Parents: %d\n", len(n22.Parents))   // 1
	fmt.Printf("Children: %d\n", len(n22.Children)) // 0

	parent := n22.Parents[0]
	n22.UnlinkFromNodeOnFieldParents(parent)
	n22.LinkToNodeOnFieldChildren(parent)
	err = sess.SaveDepth(ctx, n22, 1)
	// failed to save in auto transaction, expected relationship deletions not equal to actual. Expected=2|Actual=1
	if err != nil {
		log.Err(err).Msg("")
	}

	// Works fine if you save child and reload parent node between unlink/link

	n22 = &Node{}
	err = sess.LoadDepth(ctx, n22, n2.UUID, 1)
	if err != nil {
		log.Err(err).Msg("")
		return
	}

	parent = n22.Parents[0]
	err = n22.UnlinkFromNodeOnFieldParents(parent)
	err = sess.SaveDepth(ctx, n22, 1) 
	if err != nil {
		log.Err(err).Msg("")
	}

	err = sess.LoadDepth(ctx, parent, parent.UUID, 1)
	if err != nil {
		log.Err(err).Msg("")
	}

	n22.LinkToNodeOnFieldChildren(parent)
	err = sess.SaveDepth(ctx, n22, 1)
	if err != nil {
		log.Err(err).Msg("")
	}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions