2

我正在尝试创建一个与 Twitter 非常相似的网站。用户将能够发布消息。用户将能够相互“关注”。在主页上,他们会看到他们关注的用户的消息,按时间排序。

我该如何为此创建 appengine 模型?

在传统的关系数据库中,我想它会是这样的:

数据库“用户”:

  • ID
  • 用户名

数据库“跟随”:

  • 用户身份
  • follow_id

数据库“消息”:

  • 用户身份
  • 信息

查询将类似于:

SELECT * FROM messages m, follows f WHERE m.user_id = f.follow_id AND f.user_id = current_user_id

我想我对上面的例子很清楚。如何在 Google App Engine 中复制它?

4

2 回答 2

4

There is a useful presentation at Google I/O a while back by Brett Slatkin which describes building a scalable twitter-like microblog app, and deals with this very question at length: http://www.google.com/events/io/2009/sessions/BuildingScalableComplexApps.html

于 2011-06-07T21:15:07.487 回答
1

修改:

class AppUser(db.Model):
    user_id = db.UserProperty()
    username = db.StringProperty()
    following = db.ListProperty(db.Key) # list of AppUser keys

class Message(db.Model):
    sender = db.ReferenceProperty(AppUser)
    body = db.TextProperty()  

然后,您将分两步查询结果:

message_list = []
for followed_user in current_user.following:
    subresult = db.GqlQuery("SELECT __key__ FROM Message WHERE sender = :1", followed_user)
    message_list.extend(subresult)

results = Message.get(message_list)

(“current_user”是与您的活动用户对应的“AppUser”实体)

于 2011-06-08T00:19:49.123 回答