Using orderedlist with a secondary table

132 views
Skip to first unread message

Nathan Harmston

unread,
Jan 30, 2009, 8:15:04 AM1/30/09
to sqlal...@googlegroups.com
Hi,

I am currently trying to make a relation between Document and Author, where there is a many to many association which is dealt with by a secondary table and I am trying to store the position of an author in the author list.
mapper(Paper, document_table, properties={'journal':relation(Journal, backref='paper'),
                                          'authors':relation(Author, secondary=papers_to_authors_table,
                                                             collection_class=ordering_list('position'),
                                                             order_by=[papers_to_authors_table.c.position]),
                                          'keywords':relation(Keyword, secondary=documents_to_keywords_table)
                                          })

papers_to_authors_table = Table('p2a_association', metadata,
                                Column('document', Integer, ForeignKey('documents.document_id')),
                                Column('author', Integer, ForeignKey('authors.author_id')),
                                Column('position', Integer)
                                )

However when I try to do this:
 File "Worker.py", line 281, in <module>
    p = Paper(title, date, authors, journal, j_volume, j_issue, keywords, abstract, None, body, pmid, institution)
  File "/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/orm/attributes.py", line 1218, in init
    oldinit(instance, *args, **kwargs)
  File "/cluster/home/nathan/work/workspace/txtmining/Registry.py", line 258, in __init__
    self.authors.append(a)
  File "/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/orm/collections.py", line 922, in append
    fn(self, item)
  File "/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/ext/orderinglist.py", line 214, in append
    self._order_entity(len(self) - 1, entity, self.reorder_on_append)
  File "/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/ext/orderinglist.py", line 202, in _order_entity
    have = self._get_order_value(entity)
  File "/cluster/soft/Linux_2.6_64/lib/python2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/sqlalchemy/ext/orderinglist.py", line 189, in _get_order_value
    return getattr(entity, self.ordering_attr)
AttributeError: 'Author' object has no attribute 'position'

So is there an easy way of performing this functionality using the ordered list or do I need to do something else?

Many thanks in advance

nathan

King Simon-NFHD78

unread,
Jan 30, 2009, 9:02:30 AM1/30/09
to sqlal...@googlegroups.com
> -----Original Message-----
> From: sqlal...@googlegroups.com
> [mailto:sqlal...@googlegroups.com] On Behalf Of Nathan Harmston
> Sent: 30 January 2009 13:15
> To: sqlal...@googlegroups.com
> Subject: [sqlalchemy] Using orderedlist with a secondary table
>
> Hi,
>
> I am currently trying to make a relation between Document and
> Author, where there is a many to many association which is
> dealt with by a secondary table and I am trying to store the
> position of an author in the author list.
>

[snip]

>
> So is there an easy way of performing this functionality
> using the ordered list or do I need to do something else?
>
> Many thanks in advance
>
> nathan
>
>

You can't use the 'secondary' mapper argument if you want to be able to
use columns in that secondary table. Instead, you need to map a class to
your secondary table directly, such that it has 1-many relations with
the Document and Author classes. You can then use the 'associationproxy'
extension to hide the details.

See the docs at
http://www.sqlalchemy.org/docs/05/mappers.html#association-pattern and
http://www.sqlalchemy.org/docs/05/reference/ext/associationproxy.html#as
sociationproxy

Hope that helps,

Simon

Reply all
Reply to author
Forward
0 new messages