On Mar 8, 10:57 am, "Adam Hoscilo" <[EMAIL PROTECTED]> wrote:
> Hi, I'm starting with SA so maybe I'm missing something.
> I've checked the docs few times but I didn't find a solution.
> I would like to have a many-to-many relation through a table.
> I have Feeds they are related many-to-many with Categories and one-to-
> many with FeedItems. I haven't found any reasonable solution for this.
> Here is the code (only keys).
> ###
> feeds_table = Table('feeds', meta,
> Column('id', Integer(),primary_key=True),
> )
>
> feeditems_table = Table('feed_items', meta,
> Column('id', Integer(), primary_key=True),
> Column('feed_id', Integer(), ForeignKey('feeds.id')),
> )
>
> categories_table = Table('categories', meta,
> Column('id', Integer(), primary_key=True),
> )
>
> feedscategories_table = Table('feeds_categories', meta,
> Column('feed_id', Integer(), ForeignKey('feeds.id')),
> Column('category_id', Integer(), ForeignKey('categories.id'))
> )
>
> feeds_mapper = assign_mapper(session_context, Feed, feeds_table,
> properties = {
> 'feeditems' : relation(FeedItem, backref='feed')
> }
> )
>
> categories_mapper = assign_mapper(session_context, Category,
> categories_table, properties = {
> 'feeds' : relation(Feed, secondary=feedscategories_table,
> lazy=False, backref='categories')
> }
> )
>
> feeditems_mapper = assign_mapper(session_context, FeedItem,
> feeditems_table
> #,properties = { 'categories' :
> relation(????, lazy=False, backref='feeditems') }
dont try to map "categories" on the FeedItem mapping, its not a
database mapping. add a property to your FeedItem class:
class FeedItem(object):
def _get_categories(self):
return self.feed.categories
categories = property(_get_categories)
> Second thing: do you plan to add Feed.select_by(title=['first',
> 'second']) as an option? I know i can use and_(table.c.title="first,
> table.c.title="second").
> This feature would be convenient (maybe there is a better way that I
> don't know about).
Feed.select(Feed.c.title.in_("first", "second"))
you can also use that clause in select_by() before the keyword
arguments.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
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
-~----------~----~----~----~------~----~------~--~---