Hi,

I ran into an error trying to use two mappers for the same class. I
encounter the error "AttributeError: type object 'MyObject' has no
attribute 'my_attribute'". Using the first mapper before creating the
other seems to corrupt the mapped class. Here is an example:

from sqlalchemy import *
from sqlalchemy.orm import *

uri = r'sqlite:///bug.db'

metadata = MetaData()

class AA(object):
    pass


class BB(object):
    pass


aa_table = Table('aas', metadata,
    Column('id', Integer, primary_key=True),
    Column('stuff', Integer, default=1))

bb_table = Table('bbs', metadata,
    Column('id', Integer, primary_key=True),
    Column('other_stuff', Integer, default=2),
    Column('aa_id', Integer, ForeignKey('aas.id')))

aa_first_mapper = mapper(AA, aa_table, entity_name='first')
bb_first_mapper = mapper(BB, bb_table, entity_name='first')

aa_first_mapper.add_property('bbs', relation(bb_first_mapper,
backref='aa'))


def fill_db():
    metadata.connect(uri)
    metadata.bind.echo = False
    session = create_session()

    metadata.drop_all()
    metadata.create_all()

    aa1 = AA()
    bb1 = BB()
    bb1.aa = aa1
    bb2 = BB()
    bb2.aa = aa1
    session.save(aa1, entity_name='first')
    session.save(bb1, entity_name='first')
    session.save(bb2, entity_name='first')

    session.flush()
    session.clear()

fill_db()

def access_db():
    session = create_session()
    aas = session.query(aa_first_mapper).select()
    print 'bbs in aas[0] count =', len(aas[0].bbs)

access_db()

# commenting these 3 lines of code will fix the bug!?
aa_second_mapper = mapper(AA, aa_table, entity_name='second')
bb_second_mapper = mapper(BB, bb_table, entity_name='second')
aa_second_mapper.add_property('bbs', relation(bb_second_mapper,
backref='aa'))

access_db() # crash :(


>From what I understand of the documentation, I should be able to do
this using different entity names for the different mappers. Or is it
that once an object has been committed to the db using a certain
entity name, it can only be manipulated with this same entity name?

Thanks


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to