[google-appengine] I'm new: how should I structure 3 linked "tables"?

4 views
Skip to first unread message

Cliff

unread,
May 8, 2010, 10:44:27 AM5/8/10
to Google App Engine
Like I said in the subject, I'm new to app engine and I'm trying to
get my mind in the app engine mode, so pardon my ignorance. I'm using
the java implementation of GAE since I'm also using GWT.

I've done a lot of work over the years with mysql, sql server, etc...
and I think that the knowledge of those systems is clouding my
understanding of GAE. So, please, your help would be greatly
appreciated.

What I'm trying to do is enable tagging in a task management system
that I'm writing. The objective is to allow users to tag tasks but
also, to reduce duplicate tags in the system, have a shared tag table.
What will happen is that when a user tags a task the system would
check to see if the tag already exists in the shared table. If the tag
exists then the user's account and the current task would be linked to
the tag. If the tag doesn't already exist then it would be created and
the user's account and the current task would be linked to the newly
created tag.

I plan on linking the user's account to the tag so that the system
could list all tags associated with their account so that they could
filter the task list by selecting tags, similar to gmail's filter by
tags feature.

So basically, in MySQL I would structure these tables like so:

Account
-------------------
ID
EMail
UserName
ETC...

AccountTagLink
----------------------------
ID
AccountID
TagID

Task
------------------------------
ID
Description
ETC...

TaskTagLink
---------------------------------
ID
TaskID
TagID

Tag
-------------------------------
ID
Description

And the linkages
-----------------------------
Account.ID > AcctTagLink.AccountID
AcctTagLink.TagID > Tag.ID

Task.ID > TaskTagLink.TaskID
TaskTagLink.TagID > Tag.ID

With the above structure I could easily perform a subquery in MySQL
and get all Tags associated with an account. Ex: select * from Tag
where exists (select id from AcctTagLink where AcctTagLink.TagID =
Tag.ID and AcctTagLink.AccountID = <current account id>)

...And I could do a similar query to get all tags associated with a
given task.

I'm not asking for an equivalent structure in GAE (however that would
be nice). I just posted all that so that what I'm trying to do is
perfectly clear. So, my question to you is simply this, "How should I
structure my data in GAE to accomplish the same end result?"

Thanks for reading my wall of text, and thanks in advance for any
help :D

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Herbert

unread,
May 9, 2010, 9:57:58 PM5/9/10
to Google App Engine
Hi Cliff,

I'm no expert but would like to share some experiences. I've never
used the java version so I'll write in Python, sorry for the
inconvenice. One way is to use the stringlist property

################################
class User()
name = StringPoperty()

class Task()
name = StringProperty()
tags = StringListProperty()
owner = ReferenceProperty(User)

# write
u = User()
u.put()

t = Task()
t.tags = ['tag1', 'tag2', 'tag3', 'tag4', 'tag5']
t.owner = u
t.put()

# read
key = Key("key_of_user")
tag = 'tag3'
tasks = Task.all().filter("owner =", key).filter("tags =",
tag).fetch(10, 0)

#############################

taht's it!. if you have a long tags list, you could further do the
following for optimization:

##########################
# definations
class Task
name = StringProperty()

class Tags
tags = StringListProperty()

#write
t = Task()
t.put()

tags = Tags(parent=t)
tags = ['lots', 'of', 'tags']

# read
keys = Tags.(keys_only=True).filter("tags =", 'tag').fetch(10, 0)
keys = [k.parent() for k in keys]

tasks = db.get(keys)
##########################

the above is covered with demos and explanations in this video, great
talk:
http://www.youtube.com/watch?v=AgaL6NGpkB8

hope that helps!

Herbert
Reply all
Reply to author
Forward
0 new messages