HowTo extend base Entity class and query it's subclasses?

22 views
Skip to first unread message

Serega.Sheypak

unread,
Nov 23, 2008, 1:02:05 PM11/23/08
to Google App Engine
For example I have an entity:
AbstractRecord, it has fields:
String name, Date publishDate, Author recordAuthor, List tagList.

And I have several subclasses:
BlogRecord, CommentRecord, AdvertiseRecord.

They extend AbstractRecord, they have their own specific fields.

Can I make a query: "Select * from AbstractRecord where tagList =
'google' "
And get all AbstractRecord entites, also its subclasses which have tag
'google' in List field.

I've read
http://groups.google.com/group/google-appengine/browse_thread/thread/cadc90001d575217/1d0f86ab618f292a?lnk=gst&q=extend+entity#1d0f86ab618f292a

Seems like I can't do it?



Serega.Sheypak

unread,
Nov 23, 2008, 1:02:05 PM11/23/08
to Google App Engine

Berk D. Demir

unread,
Nov 23, 2008, 8:23:42 PM11/23/08
to Google App Engine

On Nov 23, 8:02 pm, "Serega.Sheypak" <Serega.Shey...@gmail.com> wrote:
> For example I have an entity:
> AbstractRecord, it has fields:
> String name, Date publishDate, Author recordAuthor, List tagList.
>
> And I have several subclasses:
> BlogRecord, CommentRecord, AdvertiseRecord.
>
> They extend AbstractRecord, they have their own specific fields.
>
> Can I make a query: "Select * from AbstractRecord where tagList =
> 'google' "
> And get all AbstractRecord entites, also its subclasses which have tag
> 'google' in List field.

Sorry, you can't do it.

It 's because Datastore uses different entity kinds for your inherited
model classes.
Translation is simple: Entity Kind = Name of your Model class
Datastore is not an object oriented database and from its point of
view, these records are for different entity kinds.

On the other hand, datastore doesn't require a central schema for your
entities of same type.
Instead of trying to use inheritance with db.Model, switch to Expando,
define your base properties again
and add a "record_type" property. In expando, all properties are
dynamic so add whatever needed in your control flow.
Record Type will help you to discriminate your records in the query.

If you want model level validations, you have to roll them manually.

In case you desire to use inheritance for whatever kind of plausible
reason, you might try create your own inheritable db.Expando and fix
the entity naming behavior with some monkey patching (http://
en.wikipedia.org/wiki/Monkey_patch)

Good luck!
Reply all
Reply to author
Forward
0 new messages