gcv / appengine-magic

A library designed to make it easy to use Google App Engine from Clojure
MIT License
341 stars 49 forks source link

query :entity-record-type broken #40

Closed thorwil closed 13 years ago

thorwil commented 13 years ago

I found no way to actually restrict the query to an entity-record-type within one kind.

Given (ds/defentity Article [title, body, created, updated] :kind "Post") (ds/defentity Comment [parent, index, author, link, body, created, updated] :kind "Post")

2 Articles stored and no Comments (and 2 other instances or another entity/kind):

tlog.models=> (ds/query :kind "Post" :count-only? true)
2
tlog.models=> (ds/query :kind "Post" :entity-record-type Comment :count-only? true)
2
tlog.models=> (ds/query :kind "Post" :entity-record-type Article :count-only? true)
2
tlog.models=> (ds/query :entity-record-type Comment :count-only? true)             
4
tlog.models=> (ds/query :entity-record-type Article :count-only? true)
4
tlog.models=> (ds/query :kind Article :entity-record-type Article :count-only? true)
0
tlog.models=> (ds/query :kind "Article" :entity-record-type Article :count-only? true)
0
tlog.models=> (ds/query :kind "Article" :entity-record-type "Article" :count-only? true)
0

I guess the workaround will be filtering for a non-nil property present in one type, but not the other.

gcv commented 13 years ago

Right, that isn't supported. :kind refers specifically to the "kind of entity stored in the datastore." You can think of "kinds" as "tables." If two entity types share a "kind," they go into the same table. This is just how App Engine behaves. By default, omitting the kind should treat the entity type as the kind (this is a behavior provided by appengine-magic). :entity-record-type is just provided so when you query stuff out, it goes into the correct record type.

In your example, I'm not sure why Article and Comment have the same :kind.

thorwil commented 13 years ago

Ah, I thought :entity-record-type would be about what is being queried for, too. So I though :kind "Post" for Article and Comment would allow me to very easily query among either or both.

Thanks for clearing this up!