In this specific scenario, the tag sets each live a separate table sibling 
to the table they're representing, so it's not trivial to query over all 
tags across all tables.

One thing you could do is UNION all the tag tables. To do this dynamically, 
you'd need to keep track of all the tag tables. One way to do this is to 
accumulate this list in some static variable right after the ``cls.TagClass 
= Tag`` line, and use that to form a UNION query.

Or an easier but hackier way would be to just traverse all tables and pick 
out the ones prefixed with tag_...

Or perhaps this kind of schema is not ideal for your use case. :)

- Andrey

On Thursday, May 10, 2012 12:15:55 AM UTC-7, Ciaran Farrell wrote:
>
> Ok, got it thanks. And how would you query the tags then? 
> session.query(TagMix) won't work - neither will session.query(User). Is 
> there a way of querying all tags, irrespective of what table they are 
> 'attached' to?
>
> On Wednesday, 2 May 2012 14:18:21 UTC+2, Dave wrote:
>>
>>
>> Nope, just use the one inside the scope, TagMixin.TagClass, like so for 
>> your example:
>>
>> session.add(User.TagClass(tagged=doc1,name='foo'))
>>
>>
>> On May 2, 2012, at 5:07 AM, Ciaran Farrell <[email protected]> 
>> wrote:
>>
>> But how would you actually add a tag? For example, say, using the example 
>> you provided below, I had a table called Document, which has is 'taggable'. 
>> If I create a Document object (doc1), I can see doc1.tags, which is a list. 
>> However, how do I actually _add_ a tag to doc1? I have a TagMixin object 
>> available in dir() but no Tag object (which I would have expected) - though 
>> a Tag class exists _inside_ the scope of the TagMixin. Do I have to create 
>> a Tag object outside the TagMixin class too?
>>
>> On Thursday, 28 April 2011 00:59:59 UTC+2, Andrey Petrov wrote:
>>>
>>> Ah I was really close. This worked:
>>>
>>>
>>> class TagMixin(object):
>>>     @declared_attr
>>>     def tags(cls):
>>>         class Tag(BaseModel):
>>>             __tablename__ = "tag_%s" % cls.__tablename__
>>>
>>>             id = Column(types.Integer, primary_key=True)
>>>             time_created = Column(types.DateTime, default=datetime.now, 
>>> nullable=False)
>>>
>>>             row_id = Column(types.Integer, ForeignKey(cls.id), 
>>> index=True)
>>>             name = Column(types.String, nullable=False, index=True)
>>>
>>>         cls.TagClass = Tag
>>>
>>>         return orm.relationship(Tag, backref='tagged')
>>>
>>>
>>>
>>> class User(BaseModel, TagMixin):
>>>     __tablename__ = 'user'
>>>
>>>     id = Column(types.Integer, primary_key=True)
>>>     ...
>>>
>>>
>>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "sqlalchemy" group.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msg/sqlalchemy/-/rSjfceG4OQEJ.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to 
>> [email protected].
>> For more options, visit this group at 
>> http://groups.google.com/group/sqlalchemy?hl=en.
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/-QuEVk7epL4J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to