产品评论的 GQL 数据存储模型 - 键列表?还是祖先?
我正在使用 Google App Engine 提供产品评论服务,我想知道构建我的数据存储模型的良好实践(我正在使用“高复制”设置,并且我已经收集了构建实体和他们的祖先非常重要)。特别是,我想知道是否使用键列表(或者我是否应该使用祖先/父母)。我的基本模型如下所示:
-------------
- Product -
------------- ----------
- [reviews] - ----> - Review -
- - / ---------- -------------
------------- | - author - ---> - User -
| - - -------------
| ---------- - [reviews] - --|
| - - |
| ------------- |
| |
--------------------------------------
如您所见,每个产品都包含一个评论列表,每个用户帐户还包含一个由用户撰写的评论列表。我的主要问题是关于这些评论列表,以及是否最好将它们存储为评论键列表,或者是否最好使用查询即时生成它们。
似乎对于与给定产品相关的评论列表,最好的办法可能是将评论实体创建为给定产品的子项,然后只创建评论列表 -当我需要使用祖先查询时,例如:
reviews = db.GqlQuery("SELECT * "
"FROM Review "
"WHERE ANCESTOR IS :1 "
"ORDER BY date DESC LIMIT 10",
product_key)
但是,为了获取给定用户撰写的评论列表,我想不出一种方法可以执行与上面相同的操作。看来我必须存储与用户撰写的每条评论相关的键列表。这是正确的吗?如果是这样,在 GAE 数据存储中存储密钥列表的最佳方法是什么?
对于与 GAE 数据存储和/或通用数据库设计相关的此场景的任何帮助或见解,我们将不胜感激。一般来说,我对数据库设计还很陌生,所以帮助越多越好。谢谢!
I'm making a product review service using Google App Engine, and I'm wondering about good practice for structuring my datastore models (I'm using the "high replication" setting, and I've gathered that the way you structure entities and their ancestors is quite important). Particularly, I'm wondering about using lists of keys (or if I should be using ancestors/parents instead). My basic model looks like this:
-------------
- Product -
------------- ----------
- [reviews] - ----> - Review -
- - / ---------- -------------
------------- | - author - ---> - User -
| - - -------------
| ---------- - [reviews] - --|
| - - |
| ------------- |
| |
--------------------------------------
As you can see, each product contains a list of reviews, and each user account also contains a list of reviews written by the user. My main questions are about these lists of reviews and whether it's best to store them as lists of review keys, or whether it's better to just generate them on the fly using queries.
It seems that for the list of reviews related to a given product, the best thing to do might be to create the Review entities as children of the given Product, and then just create the list of reviews on-the-fly when I need it using an ancestor query, e.g.:
reviews = db.GqlQuery("SELECT * "
"FROM Review "
"WHERE ANCESTOR IS :1 "
"ORDER BY date DESC LIMIT 10",
product_key)
However, for getting a list of reviews written by a given user, I can't think of a way to do the same as above. It seems I would have to store a list of keys that pertain to each review the user has written. Is this correct? If so, what's the best way to store a list of keys in GAE datastore?
Any help or insight into this scenario as it relates to GAE datastore and/or general database design would be greatly appreciated. I'm pretty new to database design in general, so the more help the better. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议两者都不做 - 相反,使用外键。如果评论具有指向其所针对的产品和撰写评论的用户的
db.ReferenceProperty
,您可以轻松获取产品的所有评论 (Product.review_set.all()
code>)、用户撰写的所有评论 (User.review_set.all()
) 以及评论的产品和用户。I would suggest doing neither - instead, use foreign keys. If the Review has a
db.ReferenceProperty
to the product it's for and the user who wrote it, you can easily fetch all the reviews for a product (Product.review_set.all()
), all the reviews a user has written (User.review_set.all()
), and the product and user for a review.