yes please see the "polymorphic association" example included in the 
distribution .

On Jun 21, 2010, at 3:44 PM, Ryan I. wrote:

> How would one go about modeling a relationship where one class can be
> a child of many classes. For example, let's say I have a lot of
> classes like this:
> 
>    class Cat(Base):
>        __tablename__ = 'cats'
>    class Dog(Base):
>        __tablename__ = 'dogs'
>    class Mouse(Base):
>        __tablename__ = 'mice'
>    class Bird(Base):
>        __tablename__ = 'birds'
> 
> Now, I want to tag instances of all those classes, but I want to use
> just one class and one table to store the tags. I want to aviod having
> CatTag, DogTag, MouseTag classes. I guess I can store the tags in a
> table like this:
> 
>    tags_table = Table('tags', metadata,
>        Column('id', Integer, primary_key=True),
>        Column('type', UnicodeText),
>        Column('object_id', Integer,
>        Column('name', UnicodeText))
> 
> where `type` can be one of 'cat', 'dog', 'mouse', etc, and `object_id`
> is a foreign key to a coresponding table. Is there a way to model this
> with SQLAlchemy's ORM, so that I can simply write:
> 
>    tag1, tag2 = Tag(type='dog', name='tag1'), Tag(type='dog',
> name='tag2')
>    spotty = Dog()
>    spotty.tags = [tag1, tag2]
> 
> and that SQLAlchemy does the right thing?
> 
> Thanks guys...
> 
> ~ Ryan I.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> 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 post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to