我目前正在撰写一份申请,允许人们储存图像,然后打上这些图像。 I m 和Peewee ORM(http://charlesleifer.com/docs/peewee/),这与Django s ORM非常相似。
My data model looks like this (simplified):
class Image(BaseModel):
key = CharField()
class Tag(BaseModel):
tag = CharField()
class TagRelationship(BaseModel):
relImage = ForeignKeyField(Image)
relTag = ForeignKeyField(Tag)
现在,我从概念上理解,如何询问所有有一套特定标签的形象:
SELECT Image.key
FROM Image
INNER JOIN TagRelationship
ON Image.ID = TagRelationship.ImageID
INNER JOIN Tag
ON TagRelationship.TagID = Tag.ID
WHERE Tag.tag
IN ( A , B ) -- list of multiple tags
GROUP BY Image.key
HAVING COUNT(*) = 2 -- where 2 == the number of tags specified, above
然而,我也希望能够进行更为复杂的搜索。 具体来说,我能够具体列出一份“所有标签”清单,即图像必须包含所有具体标签,并附上一份“任何”清单和一份“不”清单。
http://www.un.org。 我愿澄清这一点。 具体而言,上述问题是“所有标签”式的查询。 它回归了所有方面的形象。 我想能够具体说明如下一些情况:“我所有有标签(绿色、山区)的图像,任何一张标(背井、景观)但不是标(数字、绘画)的图像”。
Now, ideally, I d like this to be one SQL query, because pagination then becomes very easy with LIMIT and OFFSET. I ve actually got an implementation working whereby I just load everything into Python sets and then use the various intersection operators. What I m wondering is if there s a method of doing this all at once?
Also, for those interested, I ve emailed the author of Peewee about how to represent the above query using Peewee, and he responded with the following solution:
Image.select([ key ]).group_by( key ).join(TagRelationship).join(Tag).where(tag__in=[ tag1 , tag2 ]).having( count(*) = 2 )
或者,更短的版本:
Image.filter(tagrelationship_set__relTag__tag__in=[ tag1 , tag2 ]).group_by(Image).having( count(*) = 2 )
提前感谢您。