SQLAlchemy introspect column type with inheritance

前端 未结 3 489
自闭症患者
自闭症患者 2021-02-12 15:06

Considering this code (and using SQLAlchemy 0.7.7):

class Document(Base):
    __tablename__ = \'document\'
    __table_args__ = {
        \'schema\': \'app\'
            


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-12 15:55

    You need the abstract special directive or the mixin pattern.

    For a mixin, you'd use something like this:

    class MyMixin(object):
        __tablename__ = 'document'
        __table_args__ = {
            'schema': 'app'
        }
    
        id = Column(types.Integer, primary_key=True)
        nom = Column(types.Unicode(256), nullable=False)
        date = Column(types.Date())
    
    class Arrete(MyMixin, Base):
        __tablename__ = 'arrete'
    
        __mapper_args__ = {'polymorphic_identity': 'arrete'}
    
        foreign_id = Column(types.Integer, ForeignKey('app.document.id'), primary_key=True)
        numero_arrete = Column(types.Integer)
        date_arrete = Column(types.Date())
    
    
    class Document(MyMixin, Base):
        __tablename__ = 'Document'
        type_document = Column(types.Enum('arrete', 'photographie',
            name='TYPES_DOCUMENT_ENUM'))
        __mapper_args__ = {'polymorphic_on': type_document}
    

    Shared stuff goes in the mixin, non shared stuff in the sub classes.

提交回复
热议问题