On Aug 1, 2011, at 8:34 PM, somewhatofftheway wrote:

> Hi,
> 
> I'm trying to convert a 'simple' many-to-many relationship to an
> association object in order to allow the relationship to have
> attributes. I've followed the code in examples/association/
> proxied_association.py fairly closely (or so I thought) but it isn't
> working for me.
> 
> As an example, let's say I am trying to create a relationship between
> an RSS feed and the pages from the feed but I want a feed_date on the
> relationship. Currently, my code look something like this:
> 
> class Page (Base):
>        __tablename__ = 'pages'
>        id = Column(Integer, primary_key=True)
> 
>        posts = relationship("Post", cascade="all, delete-orphan",
>                            backref='pages')
>        feeds = association_proxy("feeds", "feed")
> 
> 
> class Post(Base)
>        __tablename__ = 'pages_feeds'
>        page_id = Column(Integer, ForeignKey('pages.id'),
> primary_key=True)
>        feed_id = Column(Integer, ForeignKey('feeds.id'),
> primary_key=True)
>        feed = relationship(Feed, lazy='joined')
> 
> I haven't changed the Feed class at all.
> 
> When I do something along the lines of:
> 
> page = Page()
> feed = Feed()
> page.feeds.append(feed)
> 
> the query that is issued is as follows:
> 
> IntegrityError: (IntegrityError) pages_feeds.feed_id may not be NULL
> u'INSERT INTO pages_feeds (page_id) VALUES (?)' (1,)
> 
> So, clearly I have missed out the part of the config that explains
> that adds in the second foreign key. Could anybody point me to where
> please?

you need a constructor and/or creator that generates Post() with the "feed" 
attached to it, like def __init__(self, feed): self.feed = feed, then the assoc 
proxy with creator=Post.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@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