GAE error invalid entity type

3,383 views
Skip to first unread message

Oleksandr Bezhan

unread,
May 26, 2012, 6:49:10 AM5/26/12
to golang-nuts
Hi all. I'm trying to save and retrieve entity from datastore and I've
faced with error "invalid entity type".
Here is my code:

// entity
type Product struct {
Name string
// I save generated by datastore ID in entity field
Id int64
}

// saves product to datastore
func saveProduct(c *appengine.Context, product *Product) (key
*datastore.Key, err error) {
// put entity to datastore using incomplete key and get generated key
key, err = datastore.Put(*c, datastore.NewIncompleteKey(*c,
"Product", nil), product)
panicIfErr(err)
// save generated ID to entity
product.Id = key.IntID()
key, err = datastore.Put(*c, key, product)
panicIfErr(err)
return
}

// retrieves entity from datastore using id
func GetProduct(c *appengine.Context, id int64) (product *Product, err
error) {
// restore key using id and get entity
err = datastore.Get(*c, datastore.newKey(*c, "Product", "", id, nil),
product)
// ~~~ here err is 'invalid entity type' ~~~
return
}

I'm confused cause I provide right entity type 'Product'. Maybe I
don't understand something ?
Maybe I restore entity key wrong ? How can I restore key having
generated ID ?

David Symonds

unread,
May 26, 2012, 7:08:58 AM5/26/12
to Oleksandr Bezhan, golang-nuts
On Sat, May 26, 2012 at 8:49 PM, Oleksandr Bezhan
<oleksand...@gmail.com> wrote:

> // retrieves entity from datastore using id
> func GetProduct(c *appengine.Context, id int64) (product *Product, err
> error) {
>        // restore key using id and get entity
>        err = datastore.Get(*c, datastore.newKey(*c, "Product", "", id, nil),
> product)
>        // ~~~ here err is 'invalid entity type' ~~~
>        return
> }

The error could probably be clearer, but it's most likely because
product is a nil pointer. You need it pointing at something allocated
for datastore.Get to populate.


Dave.

Oleksandr Bezhan

unread,
May 26, 2012, 8:37:30 AM5/26/12
to golang-nuts
I thought about it and tried to init product pointer with following
code:
product := &Product{}
But I got another error: "invalid memory address or nil pointer
dereference".

On 26 Тра, 14:08, David Symonds <dsymo...@golang.org> wrote:
> On Sat, May 26, 2012 at 8:49 PM, Oleksandr Bezhan
>

David Symonds

unread,
May 26, 2012, 8:24:14 PM5/26/12
to Oleksandr Bezhan, golang-nuts
On Sat, May 26, 2012 at 10:37 PM, Oleksandr Bezhan
<oleksand...@gmail.com> wrote:

> I thought about it and tried to init product pointer with following
> code:
> product := &Product{}
> But I got another error: "invalid memory address or nil pointer
> dereference".

You don't show your code for that, but I bet you have two "product"
variables; you are populating one, but returning the other.


Dave.

Oleksandr Bezhan

unread,
May 27, 2012, 4:22:49 AM5/27/12
to golang-nuts
I made it working. Just initialized pointer in other way:
product := new(Product)
instead of
product := &Product{}

On 27 Тра, 03:24, David Symonds <dsymo...@golang.org> wrote:
> On Sat, May 26, 2012 at 10:37 PM, Oleksandr Bezhan
>

Jan Mercl

unread,
May 27, 2012, 4:28:34 AM5/27/12
to Oleksandr Bezhan, golang-nuts

Dne 27.5.2012 10:22 "Oleksandr Bezhan" <oleksand...@gmail.com> napsal(a):


>
> I made it working. Just initialized pointer in other way:
> product := new(Product)
> instead of
> product := &Product{}

That sounds strange, it should be completely equivalent.

-j

Dan Kortschak

unread,
Aug 23, 2012, 6:50:19 AM8/23/12
to phili...@gmail.com, golan...@googlegroups.com, Oleksandr Bezhan
The first returns a pointer to a SheetKeyStore that has been allocated, the second returns a SheetKeyStore. This is in the spec and the tutorials.

On 23/08/2012, at 4:38 PM, "phili...@gmail.com" <phili...@gmail.com> wrote:

Hi,

I was getting invalid entity type with 
sheetKeyStore := new(SheetKeyStore)

So I changed it to
sheetKeyStore := SheetKeyStore{}

and it works.

Wow, what is wrong here? thats insane that I had to do trial and error to make it work.
Reply all
Reply to author
Forward
0 new messages