I'm trying to make three `Guy', `Father' and `Father1980' models mapped to 
two `guys' and `fathers' tables.  Fathers will inherit Guys and Father1980 
will inherit Fathers.  So I coded it as:

from sqlalchemy import (Table, Binary, Column as C, String, Integer,
                        ForeignKey, create_engine, MetaData)
from sqlalchemy.orm import (mapper, relationship, backref, scoped_session,
                            sessionmaker)

metadata = MetaData()
db_engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(bind=db_engine))

class Guy(object):
    pass

class Father(Guy):
    def __init__(self, birth):
        self.birth = birth
        super(Father, self).__init__()

class Father1980(Father):
    def __init__(self, *args, **kwargs):
        super(Father1980, self).__init__(1980, *args, **kwargs)

guys = Table('guys', metadata,
             C('id', Integer, primary_key=True),
             C('type', String, nullable=False)
)

fathers = Table('fathers', metadata,
                C('id', ForeignKey('guys.id'), primary_key=True),
                C('birth', Integer)
)

father80s = Table('fathers_80s', metadata,
                   C('id', ForeignKey('fathers.id'), primary_key=True),
                   C('birth', Integer)
)

guysMapper = mapper(Guy, guys, polymorphic_on=guys.c.type)

fathersMapper = mapper(Father, fathers, inherits=guysMapper,
                       polymorphic_identity='plain-father')

mapper(Father1980, fathers, inherits=fathersMapper,
       polymorphic_identity='young-father')

metadata.create_all(bind=db_engine)
f1 = Father(1960)
db_session.add(f1)

f2 = Father1980()
db_session.add(f2)

db_session.commit()

But this will throw cryptic (to me) error:

Traceback (most recent call last):
  File "mods.py", line 44, in <module>
    polymorphic_identity='young-father')
  File "/venv/lib/python2.7/site-packages/sqlalchemy/orm/__init__.py", line 
1208, in mapper
    return Mapper(class_, local_table, *args, **params)
  File "/venv/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 
219, in __init__
    self._configure_properties()
  File "/venv/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 
878, in _configure_properties
    self._adapt_inherited_property(key, prop, False)
  File "/venv/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 
1090, in _adapt_inherited_property
    self._configure_property(key, prop, init=False, setparent=False)
  File "/venv/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 
1117, in _configure_property
    prop.columns[0])
  File "/venv/lib/python2.7/site-packages/sqlalchemy/sql/expression.py", 
line 2737, in corresponding_column
    if self.c.contains_column(column):
  File "/venv/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", 
line 612, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/venv/lib/python2.7/site-packages/sqlalchemy/sql/expression.py", 
line 2812, in columns
    return self._columns.as_immutable()
AttributeError: 'Table' object has no attribute '_columns'



If I map `Father1980' to table `fathers_80s' instead of table `fathers' the 
error will go.  I guess mapper didn't see `guys.type' from 
`fathersMapper'.  Can I make this working?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to