There is a ghost in my code using dgraph client

There is an unknown One (1) data that is being stored without any other predicate than just uid whenever I am creating answer and comment.

// Post Table
type Post struct {
	UID              string `json:"uid,omitempty"`
...
	OwnerUserID      Users   `json:"OwnerUserID,omitempty"`
...
}

Whenever, I am creating question, It works fine.

Whenever, I am adding answers or comment, there is 2 node that is being stored. Here is one example of “answer” insert -

2 UID that are generated is:-

  • UID `0x4e32
  • UID 0x4e33

When.I query 0x4e32 like below-

{
  result(func: uid(0x4e32)) {
    uid
    expand(_all_) {
      uid
      expand(_all_)
    }
  }
}

I get this, Which is completely OK, but

{
  "data": {
    "result": [
      {
        "uid": "0x4e32",
        "ParentID": [
          {
            "uid": "0x4e29",
            "Tags": " provlem life question answer",
            "Title": "What is the main purpose?",
            "CreatedAt": "2018-05-20T13:57:09.019039+05:45",
            "PostType": "question"
          }
        ],
        "OwnerUserID": [
          {
            "uid": "0x1",
            "Name": "Alice",
            "CreatedAt": "2018-05-19T21:47:53.569504+05:45"
          }
        ],
        "CreatedAt": "2018-05-20T14:07:22.063477+05:45",
        "PostType": "answer",
        "Body": "The main purpose of test is to help the people with their provlem Four."
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 27000,
      "processing_ns": 1325000,
      "encoding_ns": 1267000
    },
    "txn": {
      "start_ts": 30119,
      "lin_read": {
        "ids": {
          "1": 79
        }
      }
    }
  }
}

But, When I query 0x4e33, I get this -

{
  "data": {
    "result": [
      {
        "uid": "0x4e33",
        "~OwnerUserID": [
          {
            "uid": "0x4e29",
            "CreatedAt": "2018-05-20T13:57:09.019039+05:45",
            "Title": "What is the main purpose?"
          }
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 33000,
      "processing_ns": 1262000,
      "encoding_ns": 1728000
    },
    "txn": {
      "start_ts": 30121,
      "lin_read": {
        "ids": {
          "1": 79
        }
      }
    }
  }
}

I have no IDEA, why the second ghost UID is created, and every time, it has only 1 predicate as in reverse edge relation to OwnerUserID

And that too, OwnerUserID is expected to User struct UID and not of Post struct.But here, the OwnerUserID is actually the UID of Post struct - How is it possible?

Can I get help on this?

This is really weird, I have just found, I have double data for each answer and comment.

Adding 1 answer/comment, getting additional 1 blank node filled with just uid and rest all blank data with reverse edge to OwnerUserID to parent data.

For example, If I am adding answer, I am getting blank node with reverse edge to Question uid as edge of name OwnerUserID

and When adding comment, same extra node with blank data except new UID with reverse edge to Answer uid as name OwnerUserID

Ok, I just found, there is One new user getting added for every post, answer, comment is added with blank data.

So, It’s clear now, Every time, I add new answer or comment to Post, it creates new Blank User with blank data.

But it also associates the post with User, I am specifying, i.e, I am telling Post of answer/comment type to assign this post to User uid of 0x1, It is doing that, but it simultaneously, also creates new User. I don’t know why?

Code to create answer and comment, that I am using is available at First post of this thread.

I can only help you with Dgraph logic. Go Lang I have no practice. Lets go:

1 - What predicates do you use to relate User => Post => answer => Comment ?
To get clearer and less confusing you should give easier relationship predicates. Even for us to understand what you want to do.

p := Post{
		CreatedAt:   &cdate,
		PostType:    "comment",
		Score:       1,
		ViewCount:   0,
		Body:        saveAnswer.Content,
		OwnerUserID: {UID: "0x1"}, 
# I believe that this should be the case here and your relationship predicate should be "OwnerUserID" and not "Users".
		ParentID: {UID: auid}, 
#This should also be just the predicate that creates the relationship between this Node and Parent.
	}

This can only happen due to mutation construction error.

I do not understand what’s going on in this line altogether, but I presume this might be the mistake. I assume it’s assigning a blank UID to this operation. You do not have to, unless you’re in the same transaction. And be using that same “blank-0” in other vital fields. Check this out.

Here I made a JSON of what would be ideal structurally speaking.

{
            "uid": "0x1",
            "Name": "Provlem.com",
            "Birth": "1988-01-01T23:00:00Z",
            "NickName": "provlem.com",
            "Email": "info@provlem.com",
            "Location": "India",
            "CreatedAt": "2018-05-19T21:47:53.569504+05:45"
            "OwnerOf":  {
                           "uid": "0x4e29",
                            "Tags": " provlem life question answer",
                            "Title": "What is the main purpose?",
                            "CreatedAt": "2018-05-20T13:57:09.019039+05:45",
                            "Score": 2,
                            "postPaid": "",
                            "postAnonymous": "",
                            "postChat": "",
                            "PostType": "question"
                       "HasAnswer": {
                                      "CreatedAt": "2018-05-20T14:07:22.063477+05:45",
                                      "Score": 2,
                                      "PostType": "answer",
                                      "Body": "The main purpose of provlem.com is to help the people with their provlem Four."
                                      "AnswerFrom": "0x32"
                               }
          }

},
1 Like

Bro, sometimes, only the comment to the right place is enough to point out the mistake, even if it is wrong. It’s enough to get the solution working.

You had correct logic, It should not be Users instead it should point to existing user and that is done using pointer.

So, I altered my code to

type Post struct {
	UID              string `json:"uid,omitempty"`
...
	OwnerUserID      *Users  `json:"OwnerUserID,omitempty"`
...
}

and while pointing user doing -

p := Post{
...
	OwnerUserID: &User{UID: "0x1"},  // & and * doing job.
...
}

Thanks bro for helping in finding the solution! You are one among the true gems in dgraph community.