hi,

i'm new to sqlalchemy, so far it seems really nice, by far the best opensource orm in python, i ran into a problem though trying to model a user system, with users and groups subclassed from principals, and a many to many relation between users and groups. i have the following code, attached but it tosses an error when trying to define the relation, not sure what to make of it. if i remove the inheritance everything works fine. should i file an issue in trac?


> python model.py
Traceback (most recent call last):
  File "model.py", line 73, in ?
properties=dict( users = relation(User.mapper, user_group_map, lazy=True, backref='groups' ) ) File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/mapping/__init__.py", line 114, in assign_mapper
    m = mapper(class_, *args, **params)
File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/mapping/__init__.py", line 49, in mapper
    return Mapper(class_, table, *args, **params)
File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/mapping/mapper.py", line 175, in __init__
    prop.init(key, self)
File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/mapping/mapper.py", line 770, in init
    self.do_init(key, parent)
File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/mapping/properties.py", line 168, in do_init
    self.secondaryjoin = sql.join(self.target, self.secondary).onclause
File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/sql.py", line 41, in join
    return Join(left, right, onclause, **kwargs)
File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/sql.py", line 834, in __init__
    self.onclause = self._match_primaries(left, right)
File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/sql.py", line 856, in _match_primaries
    if fk.references(secondary):
File "/home/hazmat/Desktop/sqlalchemy/lib/sqlalchemy/schema.py", line 53, in __getattr__
    return getattr(self._impl, key)
AttributeError: 'ColumnImpl' object has no attribute 'references'


cheers,

kapil


"""
principal base class -

 - user derives

 - group derives


many2many user  group mapping

dies when defining relation
 
"""

import sqlalchemy
from sqlalchemy import *

engine = sqlalchemy.engine.create_engine(
    'postgres',
    {'database':'example', 'user':'hazmat' },
    echo=True
    )

principals = Table(
    'principals',
    engine,
    Column('principal_id', Integer, Sequence('principal_id_seq', optional=False), primary_key=True),
    Column('name', String(50), nullable=False),    
    )

users = Table(
    'users',
    engine,
    Column('principal_id', Integer, ForeignKey('principals.principal_id'), primary_key=True),
    Column('password', String(50), nullable=False),
    Column('email', String(50), nullable=False),
    Column('login_id', String(50), nullable=False),
    )

groups = Table(
    'groups',
    engine,
    Column( 'principal_id', Integer, ForeignKey('principals.principal_id'), primary_key=True),    
    )

user_group_map = Table(
    'user_group_map',
    engine,
    Column('user_id', Integer, ForeignKey( "users.principal_id"), primary_key=True ),
    Column('group_id', Integer, ForeignKey( "groups.principal_id"), primary_key=True )    
    )

class Principal( object ):
    pass

class User( Principal ):
    pass

class Group( Principal ):
    pass

assign_mapper( Principal, principals )


assign_mapper( 
    User, 
    users,
    inherits=Principal.mapper
    )

assign_mapper( 
    Group,
    groups,
    inherits=Principal.mapper,
    properties=dict( users = relation(User.mapper, user_group_map, lazy=True, backref='groups' ) )
    )


                                




Reply via email to