On Sep 30, 2008, at 4:59 AM, Nick Murdoch wrote:
> >> we have the "wrapper" thing, its the associationproxy. docs are >> here: >> http://www.sqlalchemy.org/docs/04/plugins.html#plugins_associationproxy >> . >> >> In this case you'd probably map a class to the user_articles_table >> and >> use associationproxy to create user.articles against "article id". > > Hi again, thanks for your pointers. I'm still having trouble, > unfortunately. > > I now have the following schema set up: > > ------ > > users_table = Table('tg_user', metadata, > Column('user_id', Integer, primary_key=True), > ...) > user_articles_table = Table('user_article', metadata, > Column('user_id', Integer, ForeignKey('tg_user.user_id')), > Column('article_hash', String(32)), > ) > > def _create_ua_by_article(article): > """ A creator function. """ > return UserArticle(article_hash=article) > > class User(object): > ... > saved_articles = association_proxy('user_articles', > 'article_hash', creator=_create_ua_by_article) > > class UserArticle(object): > """ Joining relationship between Users and articles (articles > stored elsewhere) """ > def __init__(self, user=None, article_hash=None): > self.user = user > self.article_hash = article_hash > > mapper(User, users_table, properties={ > 'user_articles': relation(UserArticle), > }) > > mapper(UserArticle, user_articles_table, properties={ > 'user': relation(User), > }) > > ------ > > Unfortunately this schema won't generate the tables without > complaining about no primary key on user_articles, so I made both > columns on UserArticle a primary_key which seemed to work okay. > > So, adding articles to the user.saved_articles list seems to work, and > the database reflects this, however, trying to remove an article > raises an exception: "AssertionError: Dependency rule tried to blank- > out primary key column 'user_article.user_id' on instance > '[EMAIL PROTECTED]'" > > I've demonstrated this in the following session. If someone could > point out where I'm going wrong, I'd be most grateful :) when entries are removed from User.user_articles, the row has to be deleted. So set "cascade='all, delete-orphan'" on the relation() from User->UserArticle. Also, its better if you use a backref for the "user_articles"/"user" combination, like so: mapper(User, users_table, properties={ 'user_articles':relation(UserArticle, cascade='all, delete- orphan', backref='user') }) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
