from sqlalchemy import *
from sqlalchemy.orm import *
meta = MetaData()
#======== generated SA set-up
table_A = Table( 'A', meta,
    Column( 'name', String(length=100), ),
    Column( 'db_id',   primary_key= True,   type_= Integer, ),
)
table_AB = Table( 'AB', meta,
    Column( 'dbid', Integer, primary_key= True, ),
    Column( 'this_id', Integer, ForeignKey( 'A.db_id', )),#   autoincrement= False,   nullable= False),#,   primary_key= True, ),
#    Column( 'other_id', Integer, ForeignKey( 'B.db_id', )),#   autoincrement= False,   nullable= False),#,   primary_key= True, ),
)
if 0:
    table_B = Table( 'B', meta,
        Column( 'name', String(length=100), ),
        Column( 'db_id',   primary_key= True,   type_= Integer, ),
    )

#meta.create_all()
Base= Association=object
class A( Base):pass
class AB( Association):pass
class B( Base):pass

mapper_A = mapper( A, table_A,)
mapper_AB = mapper( AB, table_AB,)
mapper_AB.add_property( 'this', relation( A,) )
'''
            foreign_keys= table_AB.c.this_id,
            lazy= False,
            primaryjoin= table_AB.c.this_id == table_A.c.db_id,
            remote_side= table_A.c.db_id,
            uselist= False,
            backref='ab'
            ) )
'''
if 0:
    mapper_AB.add_property( 'other', relation( B,
            foreign_keys= table_AB.c.other_id,
            lazy= False,
            primaryjoin= table_AB.c.other_id == table_B.c.db_id,
            remote_side= table_B.c.db_id,
            uselist= False,
            backref='ba'
            ) )

    mapper_B = mapper( B, table_B,
            )

#========= eo generated SA set-up
#"A".name = ?
#"AB".this_id = ?
compile_mappers()
a=A()
print 'abc' == A.name   #ok
print 'abc' == A.db_id  #ok
print AB.this == A.db_id#wrong
print A.name == AB.this #error


