Date string is automatically formatted as dateTime

Moved from GitHub dgo/27

Posted by tikiatua:

What am I trying to do?

  • Saving a string that contains iso date information (i.e. “8888-01-01”) as string predicate in dgraph using the json mutation format

What is happening?

  • The string is parsed as dateTime and saved as dateTime string, i.e. 8888-01-02T00:00:00Z

What would I expect?

  • The string should not be parsed and should be saved as is “8888-01-01”.

The problem does not seem to occur if the predicate is updated from ratel using the rdf n-quad format.

srfrog commented :

@tikiatua are you still having this issue?

tikiatua commented :

Hi @sfrog

The problem is still present. I did however adopt most of my code to use the rdf format, for which I wrote my own custom json to rdf converter. I can check tomorrow if the problem is still present with the newest code.

The reason for the problem might be that the dgo package uses reflection to convert all date alike fields into a time struct. To avoid this problem, the package would need to take into account the schema definition of the respective field.

manishrjain commented :

Did you try setting the schema for the predicate upfront? Then, Dgraph should not try to convert it to date.

tikiatua commented :

Yepp. The schema is defined upfront. Interestingly everything works as intended when I use the ratel admin interface to set a predicate.

The conversion only makes problems when passing a json to the dgo client. That’s why I assume that there is some json string to go time conversion happening that is converting the date string to a date time string.

mangalaman93 commented :

@tikiatua please let us know if you are still seeing the issue.

Issif commented :

I have exact same issue with dgraph 1.1.0. I’m using dgo client like @tikiatua does :

{
        "uid": "0xd19f",
        "name": "XXX",
        "dgraph.type": [
          "SecurityGroup"
        ],
        "_Cidr": [
          {
            "name": "XXXX",
            "dgraph.type": [
              "Cidr"
            ],
            "_Cidr|PortTcp": "8983-01-01T00:00:00Z"
          },
          {
            "name": "XXXX",
            "dgraph.type": [
              "Cidr"
            ],
            "_Cidr|PortTcp": "22"
          },
          {
            "name": "XXXXX",
            "dgraph.type": [
              "Cidr"
            ],
            "_Cidr|PortTcp": "8983-01-01T00:00:00Z"
          }
        ]
      }

For both, I was supposed to be 8983 for value.

mangalaman93 commented :

@Issif @tikiatua I wrote the following test and it seems to work fine. Will you let me know what doesn’t work for you? This is on current master of both dgraph and dgo.

Result

$ go test -v -run TestJsonTestOfTime
[Decoder]: Using assembly version of decoder
=== RUN   TestJsonTestOfTime
{"q":[{"name":"A","port":"8983-01-01"},{"name":"B","port":"8083-01-01"}]}
--- PASS: TestJsonTestOfTime (0.68s)
PASS
ok  	github.com/dgraph-io/dgraph/dgraph/cmd/alpha	0.701s

Code

func TestJsonTestOfTime(t *testing.T) {
	dg, err := testutil.DgraphClient("localhost:9180")
	require.NoError(t, err, "error while getting a dgraph client")

	require.NoError(t, dg.Alter(context.Background(), &api.Operation{
		DropOp: api.Operation_ALL,
	}))
	require.NoError(t, dg.Alter(context.Background(), &api.Operation{
		Schema: `
      name: string  .
      born_at: string .
    `,
	}))

	m1 := `
    {
      "set": [
        {
          "name": "A",
          "born_at": "8983-01-01"
        },
        {
          "name": "B",
          "born_at": "8083-01-01"
        }
      ]
    }`
	req := &api.Request{
		CommitNow: true,
		Mutations: []*api.Mutation{
			{
				SetJson: []byte(m1),
			},
		},
	}
	_, err = dg.NewTxn().Do(context.Background(), req)
	require.NoError(t, err)

	q2 := `
    {
      q(func: has(name)) {
        name
        port
      }
    }
  `
	resp, err := dg.NewReadOnlyTxn().Query(context.Background(), q2)
	require.NoError(t, err)
	fmt.Println(string(resp.Json))
}

Issif commented :

@mangalaman93 I see in your test set : "born_at": "8983-01-01", for my case, the value was simply 8983, just a string, not a date pattern.

mangalaman93 commented :

@Issif in that case too, Dgraph seems to result the same string. What version of Dgraph/Dgo were you running? May be, it happens on an older version.

Issif commented :

I’m using :

  • dgo v2.1.0
  • dgraph/standalone:v1.0.17 as image

I see there’s a new image, v1.1.1, will try this.

FYI, my app is open source, you can find it here : GitHub - claranet/aws-inventory-graph: Explore your AWS platform with, Dgraph, a graph database.

mangalaman93 commented :

dgo v2.X is not compatible with dgraph v1.0.X
See the compatibility matrix here GitHub - dgraph-io/dgo: Official Dgraph Go client
Please try the v1.1.1 image and let me know if you still face this issue.

mangalaman93 commented :

Closing this issue for now, feel free to open it if you are able to reproduce the issue.