{"action":"created","issue":{"url":"https://api.github.com/repos/GoogleCloudPlatform/google-cloud-go/issues/453","repository_url":"https://api.github.com/repos/GoogleCloudPlatform/google-cloud-go","labels_url":"https://api.github.com/repos/GoogleCloudPlatform/google-cloud-go/issues/453/labels{/name}","comments_url":"https://api.github.com/repos/GoogleCloudPlatform/google-cloud-go/issues/453/comments","events_url":"https://api.github.com/repos/GoogleCloudPlatform/google-cloud-go/issues/453/events","html_url":"https://github.com/GoogleCloudPlatform/google-cloud-go/issues/453","id":194281174,"number":453,"title":"datastore: retrieving an entity's key as a struct field","user":{"login":"luisfernrg","id":16088714,"avatar_url":"https://avatars.githubusercontent.com/u/16088714?v=3","gravatar_id":"","url":"https://api.github.com/users/luisfernrg","html_url":"https://github.com/luisfernrg","followers_url":"https://api.github.com/users/luisfernrg/followers","following_url":"https://api.github.com/users/luisfernrg/following{/other_user}","gists_url":"https://api.github.com/users/luisfernrg/gists{/gist_id}","starred_url":"https://api.github.com/users/luisfernrg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/luisfernrg/subscriptions","organizations_url":"https://api.github.com/users/luisfernrg/orgs","repos_url":"https://api.github.com/users/luisfernrg/repos","events_url":"https://api.github.com/users/luisfernrg/events{/privacy}","received_events_url":"https://api.github.com/users/luisfernrg/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-12-08T09:05:18Z","updated_at":"2016-12-08T21:14:56Z","closed_at":null,"body":"I want to be able to query a my datastore and retrieve all of an entity's properties including it's key.\r\n\r\nWhat I want to be able to have a struct like this\r\n```golang\r\ntype User struct {\r\n\tKey          *datastore.Key `datastore:\"__key__\"`\r\n\tUsername     string         `datastore:\"username\"`\r\n\tPasswordHash string         `datastore:\"password_hash\"`\r\n\tCreatedAt    time.Time      `datastore:\"created_at\"`\r\n}\r\n```\r\n\r\nand be able to use a query like this\r\n\r\n```golang\r\nq := datastore.NewQuery(\"User\").Filter(\"username =\", \"gopher\").Limit(1)\r\n```\r\n\r\nand be able to get what ever user that is return and store it in a User struct. Like how it's done when using `datastore.Get` but instead of getting the entity with they key i want to be able to do so with other properties such as username or even email if I had a property like that.\r\n\r\nat the moment I'm using the datastore.GetAll function and ranging over they keys that is return from that function and with `[]User` and storing the key with it's user\r\nexample:\r\n```golang\r\n        var user []User\r\n\tq := datastore.NewQuery(\"User\").Filter(\"username =\", \"gopher\").Limit(1)\r\n\tkeys, err := datastore.GetAll(ctx, q, &user)\r\n\tif err != nil {\r\n\t\t// Handle error\r\n\t}\r\n\tfor k,v := range keys {\r\n\t\tuser[k].Key = v\r\n\t}\r\n```\r\n\r\nis there a better way of doing this or is this the correct way?"},"comment":{"url":"https://api.github.com/repos/GoogleCloudPlatform/google-cloud-go/issues/comments/265857499","html_url":"https://github.com/GoogleCloudPlatform/google-cloud-go/issues/453#issuecomment-265857499","issue_url":"https://api.github.com/repos/GoogleCloudPlatform/google-cloud-go/issues/453","id":265857499,"user":{"login":"adams-sarah","id":2741356,"avatar_url":"https://avatars.githubusercontent.com/u/2741356?v=3","gravatar_id":"","url":"https://api.github.com/users/adams-sarah","html_url":"https://github.com/adams-sarah","followers_url":"https://api.github.com/users/adams-sarah/followers","following_url":"https://api.github.com/users/adams-sarah/following{/other_user}","gists_url":"https://api.github.com/users/adams-sarah/gists{/gist_id}","starred_url":"https://api.github.com/users/adams-sarah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adams-sarah/subscriptions","organizations_url":"https://api.github.com/users/adams-sarah/orgs","repos_url":"https://api.github.com/users/adams-sarah/repos","events_url":"https://api.github.com/users/adams-sarah/events{/privacy}","received_events_url":"https://api.github.com/users/adams-sarah/received_events","type":"User","site_admin":false},"created_at":"2016-12-08T21:14:56Z","updated_at":"2016-12-08T21:14:56Z","body":"Hm ok, so again the problem is in how you are storing the data, and not in how you are querying it.\r\n\r\nSo your initial Put would need to look something like:\r\n```go\r\nuser := User{/* blah */}\r\n// create a complete key\r\nkey := datastore.IDKey(\"User\", 12345, nil)\r\n// add key to the user struct value\r\nuser.Key = key\r\n// Put User\r\n_, err = client.Put(ctx, key, &user)\r\nif err != nil {\r\n    // do something\r\n}\r\n\r\n// and now you can do your query\r\n```\r\n\r\nRemember that the Key field on User is exactly like all the other fields. Nothing special. You have to populate the field yourself before you save the User.\r\n\r\nI suspect that the reason you are not getting Keys back in your query is because they are empty in the entity in datastore.\r\n\r\nI'm sorry this is not more intuitive. I'm thinking about ways we can make the `datastore:\"__key__\"` field tag work more magically for the top level struct (as you initially thought it would).\r\n\r\nHappy to answer any more questions you might have."}}
{"actor":{"display_login":"adams-sarah"}}