add example of declared_attr columns shared across inheritance to declarative docs

Issue #2471 resolved
Mike Bayer repo owner created an issue

i.e.

class InheritMixin(object):

   @declared_attr
   def __tablename__(cls):
       return cls.__name__

   @declared_attr
   def id(cls):
       return Column(Integer, primary_key = True)

   @declared_attr
   def __mapper_args__(cls):
       if not has_inherited_table(cls):
           return {'polymorphic_on': 'discriminator'}
       else:
           return {'polymorphic_identity': cls.__name__}

class Inherits(InheritMixin):
    @declared_attr
    def id(cls):
        super_id = super(Inherits, cls).id
        return Column(Integer, ForeignKey(super_id),primary_key = True)

class Person(InheritMixin, Base):
   discriminator = Column(String(50))
   name = Column(String(50))

class Engineer(Inherits, Person):
   job = Column(String(50))

Comments (3)

  1. Mike Bayer reporter

    im a little shaky on this as the pattern doesn't work with more than one level of inheritance.

  2. Log in to comment