> 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 :) ------ >>> session.begin() <sqlalchemy.orm.session.SessionTransaction object at 0x1580550> >>> user = session.query(User).first() >>> user.user_id 1 >>> user.saved_articles ['5e4dda932fce6b9a68f370406526cb10', 'bc21f67412bca994acd1af8b1dd78942'] >>> user.saved_articles.append('f3529aac5eda60741e9e9dab7bea905d') >>> session.commit() >>> user.saved_articles ['5e4dda932fce6b9a68f370406526cb10', 'bc21f67412bca994acd1af8b1dd78942', 'f3529aac5eda60741e9e9dab7bea905d'] >>> session.begin() <sqlalchemy.orm.session.SessionTransaction object at 0x15e1890> >>> user.saved_articles.remove('f3529aac5eda60741e9e9dab7bea905d') >>> session.commit() Traceback (most recent call last): File "<console>", line 1, in ? File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/scoping.py", line 98, in do return getattr(self.registry(), name)(*args, **kwargs) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/session.py", line 554, in commit self.transaction.commit() File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/session.py", line 259, in commit self._prepare_impl() File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/session.py", line 243, in _prepare_impl self.session.flush() File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/session.py", line 786, in flush self.uow.flush(self, objects) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/unitofwork.py", line 233, in flush flush_context.execute() File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/unitofwork.py", line 445, in execute UOWExecutor().execute(self, tasks) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/unitofwork.py", line 930, in execute self.execute_save_steps(trans, task) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/unitofwork.py", line 948, in execute_save_steps self.execute_dependencies(trans, task, False) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/unitofwork.py", line 959, in execute_dependencies self.execute_dependency(trans, dep, False) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/unitofwork.py", line 942, in execute_dependency dep.execute(trans, isdelete) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/unitofwork.py", line 895, in execute self.processor.process_dependencies(self.targettask, [elem.state for elem in self.targettask.polymorphic_tosave_elements if elem.state is not None], trans, delete=False) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/dependency.py", line 193, in process_dependencies self._synchronize(state, child, None, True, uowcommit) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/dependency.py", line 249, in _synchronize sync.clear(dest, self.mapper, self.prop.synchronize_pairs) File "/usr/lib/python2.4/site-packages/SQLAlchemy-0.4.6-py2.4.egg/ sqlalchemy/orm/sync.py", line 32, in clear raise exceptions.AssertionError("Dependency rule tried to blank- out primary key column '%s' on instance '%s'" % (r, mapperutil.state_str(dest))) AssertionError: Dependency rule tried to blank-out primary key column 'user_article.user_id' on instance '[EMAIL PROTECTED]' ------ Thanks, Nick --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---