Hi all,
I reproduced an error I was having with the code below (basically
pasted from the tutorial for joined inheritance) and added an
'inherit_condition' to __mapper_args__ of the subclass. At that point
the code started throwing a sqlalchemy.exc.InterfaceError. The code
is:
---------------------------------------------------------
from sqlalchemy import *
from sqlalchemy.orm.session import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///:memory:', echo=False)
Base = declarative_base(bind = engine)
Session = sessionmaker(bind = engine)
def setup(engine):
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
return Session()
class Person(Base):
__tablename__ = 'Person'
id = Column(Integer, primary_key=True)
discriminator = Column('type', String(50))
__mapper_args__ = {'polymorphic_on': discriminator}
class Engineer(Person):
__tablename__ = 'Engineer'
__mapper_args__ = {'polymorphic_identity': 'Engineer',
'inherit_condition': (id == Person.id)}
id = Column(Integer, ForeignKey('Person.id'), primary_key=True)
if __name__ == '__main__':
session = setup(engine)
e = Engineer()
session.add_all([e])
session.commit()
print e.discriminator
--------------------------------------------
and the error:
--------------------------------------------
Traceback (most recent call last):
File "D:\Documents\Code\Eclipse\workspace\SQLAdata\src\test5.py",
line 38, in <module>
print e.discriminator
File "C:\Python27\lib\site-packages\sqlalchemy\orm\attributes.py",
line 168, in __get__
return self.impl.get(instance_state(instance),dict_)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\attributes.py",
line 451, in get
value = callable_(passive)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\state.py", line
285, in __call__
self.manager.deferred_scalar_loader(self, toload)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line
1701, in _load_scalar_attributes
only_load_props=attribute_names)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line
2378, in _load_on_ident
return q.one()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line
2050, in one
ret = list(self)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line
2093, in __iter__
return self._execute_and_instances(context)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line
2108, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line
1405, in execute
params)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line
1538, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line
1646, in _execute_context
context)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line
1639, in _execute_context
context)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\default.py",
line 330, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.InterfaceError: (InterfaceError) Error binding
parameter 0 - probably unsupported type. u'SELECT "Person".type AS
"Person_type", "Engineer".id AS "Engineer_id", "Person".id AS
"Person_id" \nFROM "Person" JOIN "Engineer" ON "Person".id = ? \nWHERE
"Person".id = ?' (<built-in function id>, 1)
---------------------------------------------------------
I fixed the error by moving the id Column in Engineer above the
__mapper_args__ :
---------------------------------------------------------
class Engineer(Person):
__tablename__ = 'Engineer'
id = Column(Integer, ForeignKey('Person.id'), primary_key=True)
__mapper_args__ = {'polymorphic_identity': 'Engineer',
'inherit_condition': (id == Person.id)}
---------------------------------------------------------
Now we get to the problem: In my code I dynamically generate tables
with type(name, (base,), dicty). In dicty are the __mapper_args__ and
id Column. Because of the dictionary used I have no control over the
order of the arguments and that seems to cause my exception. If I add
the __mapper_args__ with 'inherit_condition' to the class later, I
get another exception (ArgumentError: Can't determine join between
'person' and 'child'; tables have more than one foreign key constraint
relationship between them. Please specify the 'onclause' of this join
explicitly.)
Could the order in 'dicty' be the problem? Can anyone help me with a
solution or workaround for this problem.
Cheers, Lars
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
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.