Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to reverse child/parent relation #118

Open
lomoval opened this issue Nov 29, 2022 · 1 comment
Open

Failed to reverse child/parent relation #118

lomoval opened this issue Nov 29, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@lomoval
Copy link

lomoval commented Nov 29, 2022

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("")
	}
@lomoval lomoval added the bug Something isn't working label Nov 29, 2022
@nikitawootten
Copy link
Contributor

Thanks for the info and the complete example! I'll take a look.

@nikitawootten nikitawootten self-assigned this Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants