I was trying to do the same thing, But i wanted a different
discriminator for the second level of inheritance.
I changed mine to use the same polymorphic_on for both levels and it
seemed to work for me (although not exactly how i wanted it to be)
My suggestion is :
Get rid of your engineer_id or id in MechanicalEngineer, you don't
need both as they will be the same value.
Add " 'with_polymorphic': '*' " to the __mapper_args__ of both base
classes.
I can email you this in working form if you want.
################# Here is what worked for me ##################
Base = declarative_base()
# ================================ Actions Base
==============================
class _UtConfActions(Base):
__tablename__ = 'tblActions'
# 1 to Many relationship to the managed
id = Column(Integer, primary_key=True)
managed_id = Column(Integer, ForeignKey('tblManagedDetails.id'))
action = Column(String)
__mapper_args__ = {'polymorphic_on': action, 'with_polymorphic': '*'}
# ============================== Reconciler Actions
===========================
class _UtConfReconcilerActions(_UtConfActions):
__tablename__ = 'tblReconcilerActions'
# 1 to Many relationship to the managed
id = Column(Integer, ForeignKey('tblActions.id'), primary_key=True)
action = Column(String)
object_type = Column(String)
__mapper_args__ = {'polymorphic_identity': 'RECONCILER',
'polymorphic_on': action, 'with_polymorphic': '*'}
# ======================= Reconciler Snaphot Actions
==========================
class _UtConfReconcilerActionSnapshot(_UtConfReconcilerActions):
__tablename__ = 'tblReconcilerActionSnapshot'
__mapper_args__ = {'polymorphic_identity': 'R_SNAPSHOT'}
# Joined table inheritence
id = Column(Integer, ForeignKey('tblReconcilerActions.id'),
primary_key=True)
revision = Column(String)
comment = Column(String)
##########################################################
On Oct 1, 8:38 pm, Julian Krause <[email protected]> wrote:
> I have looked all over to a solution for this issue and haven't found
> a solution. I am trying to use inheritance more than one level deep
> and it does not seem to set the discriminator field on the top level
> item. It works fine if it is only subclassed once but when you try and
> do it twice it fails. The code below shows this happening in as short
> of an example as I could come up with.
>
> Thanks in advance for help,
>
> Julian Krause
>
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy import Column, Integer, String, ForeignKey,
> create_engine
> from sqlalchemy.orm import sessionmaker
>
> Base = declarative_base()
>
> class Person(Base):
> __tablename__ = 'people'
> id = Column(Integer, primary_key=True)
> discriminator = Column('type', String(50))
> __mapper_args__ = {'polymorphic_on': discriminator}
>
> class Engineer(Person):
> __tablename__ = 'engineers'
> id = Column(Integer, ForeignKey('people.id'), primary_key=True)
> primary_language = Column(String(50))
> discriminator = Column('type', String(50))
> __mapper_args__ = {'polymorphic_identity': 'engineer',
> 'polymorphic_on': discriminator}
>
> class MechanicalEngineer(Engineer):
> __tablename__ = 'mechanical_engineers'
> __mapper_args__ = {'polymorphic_identity': 'mechanical_engineer'}
> id = Column(Integer, ForeignKey('people.id'), primary_key=True)
> engineer_id = Column(Integer, ForeignKey('engineers.id'),
> primary_key=True)
> primary_tool = Column(String(50))
>
> engine = create_engine('sqlite:///test.db', echo=True)
> Base.metadata.drop_all(engine)
> Base.metadata.create_all(engine)
> Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
>
> sess = Session()
>
> me = MechanicalEngineer()
> sess.add(me)
> sess.commit()
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---