Association objects in 0.4.2p3

17 views
Skip to first unread message

Donovan Kolbly

unread,
Feb 12, 2008, 9:41:55 AM2/12/08
to sqlalchemy
I am trying to go through the mapping tutorial in 0.4.2p3 for
association objects. I get an error about "Could not assemble any
primary key columns for mapped table 'association'" when attempting to
map the association table itself. This is straight out of the Mapping
Configuration docs that comes with the distribution...

Any thoughts on where things are going awry?

Here's my complete code:

from sqlalchemy import create_engine, \
Table, Column, Integer, String, \
MetaData, ForeignKey
from sqlalchemy.orm import relation, sessionmaker, mapper

engine = create_engine('sqlite:///:memory:', echo=True)

metadata = MetaData();

left_table = Table('left', metadata,
Column('id', Integer, primary_key=True))

right_table = Table('right', metadata,
Column('id', Integer, primary_key=True))

association_table = Table('association', metadata,
Column('left_id', Integer, ForeignKey('left.id')),
Column('right_id', Integer, ForeignKey('right.id')),
Column('data', String(50))
)

class Parent(object): pass
class Association(object): pass
class Child(object): pass

mapper( Parent, left_table, properties={
'children':relation(Association)
})

mapper( Association, association_table, properties={
'child':relation(Child) # <--- chokes here
})

mapper( Child, right_table)

metadata.create_all( engine )

And here is what I get:

$ python -i posting.py
Traceback (most recent call last):
File "posting.py", line 31, in <module>
'child':relation(Child) # <--- chokes here
File "/tmp/SQLAlchemy-0.4.2p3/lib/sqlalchemy/orm/__init__.py", line
544, in mapper
return Mapper(class_, local_table, *args, **params)
File "/tmp/SQLAlchemy-0.4.2p3/lib/sqlalchemy/orm/mapper.py", line
160, in __init__
self._compile_pks()
File "/tmp/SQLAlchemy-0.4.2p3/lib/sqlalchemy/orm/mapper.py", line
428, in _compile_pks
raise exceptions.ArgumentError("Could not assemble any primary key
columns for mapped table '%s'" % (self.mapped_table.name))
sqlalchemy.exceptions.ArgumentError: Could not assemble any primary
key columns for mapped table 'association'
>>>

Michael Bayer

unread,
Feb 12, 2008, 11:44:52 AM2/12/08
to sqlal...@googlegroups.com

the association_table itself has no primary key columns, so you have
to tell the mapper which columns it should consider to be the primary
key:

mapper(Association, association_table, properties={...},
primary_key=[association_table.c.left_id, association_table.c.right_id])

Ill update the docs right now.


Reply all
Reply to author
Forward
0 new messages