SQLAlchemy 映射 table 列与过滤器
SQLAlchemy mapping table columns with filters
我在 PostgreSQL 中有一个 table,其中包含有关文档的信息。让我们这样说:
table: doc
id (int)
name (string)
type (int)
type 是文件的类别(例如 1 - 护照,2 - 保险等)。此外,我有不同的 tables,其中包含每种文档类型的附加信息。
table: info
id (int)
doc_id (fk)
info (additional columns)
我想要一个 SQLAlchemy 模型来处理与其附加信息链接的每种类型的文档,并能够管理要显示的列(对于 Flask-Admin,如果它很重要)。
现在将两个 table 加入某种 "model" 我使用了 SQLAlchemy 文档中的 Mapping Table Columns(当只有一种类型的文档时):
class DocMapping(db.Model):
__table__ = doc.__table__.join(info)
__mapper_args__ = {
'primary_key': [doc.__table__.c.id]
}
现在的问题是:如何基于doc.type列创建多个类继承自db.Model(DocPassportMapping、DocInsuranceMapping等)?
类似的东西:
__table__ = doc.__table__.join(info).filter(doc.type)
这显然是行不通的,因为我们这里没有 query 对象。
如果我没理解错的话,您希望在连接的映射之上有一个 inheritance hierarchy based on DocMapping
with DocMapping.type
as the polymorphic identity. Since you have not provided a complete example, here is a somewhat similar structure. It has differences for sure, but should be applicable to yours. This uses single table inheritance。
模特:
In [2]: class Doc(Base):
...: id = Column(Integer, primary_key=True, autoincrement=True)
...: name = Column(Unicode)
...: type = Column(Integer, nullable=False)
...: __tablename__ = 'doc'
...:
In [3]: class Info(Base):
...: __tablename__ = 'info'
...: doc_id = Column(Integer, ForeignKey('doc.id'), primary_key=True)
...: value = Column(Unicode)
...: doc = relationship('Doc', backref=backref('info', uselist=False))
...:
In [4]: class DocMapping(Base):
...: __table__ = Doc.__table__.join(Info)
...: __mapper_args__ = {
...: 'primary_key': (Doc.id, ),
...: # These declare this mapping polymorphic
...: 'polymorphic_on': Doc.type,
...: 'polymorphic_identity': 0,
...: }
...:
In [5]: class Passport(DocMapping):
...: __mapper_args__ = {
...: 'polymorphic_identity': 1,
...: }
...:
In [6]: class Insurance(DocMapping):
...: __mapper_args__ = {
...: 'polymorphic_identity': 2,
...: }
...:
测试:
In [7]: session.add(Insurance(name='Huono vakuutus',
...: value='0-vakuutus, mitään ei kata'))
In [8]: session.commit()
In [15]: session.query(DocMapping).all()
Out[15]: [<__main__.Insurance at 0x7fdc0a086400>]
In [16]: _[0].name, _[0].value
Out[16]: ('Huono vakuutus', '0-vakuutus, mitään ei kata')
问题是:您可能不希望继承自 db.Model
的多个 类 作为基础,而是希望继承自 DocMapping
的 类。它作为层次结构更有意义。
我在 PostgreSQL 中有一个 table,其中包含有关文档的信息。让我们这样说:
table: doc
id (int)
name (string)
type (int)
type 是文件的类别(例如 1 - 护照,2 - 保险等)。此外,我有不同的 tables,其中包含每种文档类型的附加信息。
table: info
id (int)
doc_id (fk)
info (additional columns)
我想要一个 SQLAlchemy 模型来处理与其附加信息链接的每种类型的文档,并能够管理要显示的列(对于 Flask-Admin,如果它很重要)。
现在将两个 table 加入某种 "model" 我使用了 SQLAlchemy 文档中的 Mapping Table Columns(当只有一种类型的文档时):
class DocMapping(db.Model):
__table__ = doc.__table__.join(info)
__mapper_args__ = {
'primary_key': [doc.__table__.c.id]
}
现在的问题是:如何基于doc.type列创建多个类继承自db.Model(DocPassportMapping、DocInsuranceMapping等)?
类似的东西:
__table__ = doc.__table__.join(info).filter(doc.type)
这显然是行不通的,因为我们这里没有 query 对象。
如果我没理解错的话,您希望在连接的映射之上有一个 inheritance hierarchy based on DocMapping
with DocMapping.type
as the polymorphic identity. Since you have not provided a complete example, here is a somewhat similar structure. It has differences for sure, but should be applicable to yours. This uses single table inheritance。
模特:
In [2]: class Doc(Base):
...: id = Column(Integer, primary_key=True, autoincrement=True)
...: name = Column(Unicode)
...: type = Column(Integer, nullable=False)
...: __tablename__ = 'doc'
...:
In [3]: class Info(Base):
...: __tablename__ = 'info'
...: doc_id = Column(Integer, ForeignKey('doc.id'), primary_key=True)
...: value = Column(Unicode)
...: doc = relationship('Doc', backref=backref('info', uselist=False))
...:
In [4]: class DocMapping(Base):
...: __table__ = Doc.__table__.join(Info)
...: __mapper_args__ = {
...: 'primary_key': (Doc.id, ),
...: # These declare this mapping polymorphic
...: 'polymorphic_on': Doc.type,
...: 'polymorphic_identity': 0,
...: }
...:
In [5]: class Passport(DocMapping):
...: __mapper_args__ = {
...: 'polymorphic_identity': 1,
...: }
...:
In [6]: class Insurance(DocMapping):
...: __mapper_args__ = {
...: 'polymorphic_identity': 2,
...: }
...:
测试:
In [7]: session.add(Insurance(name='Huono vakuutus',
...: value='0-vakuutus, mitään ei kata'))
In [8]: session.commit()
In [15]: session.query(DocMapping).all()
Out[15]: [<__main__.Insurance at 0x7fdc0a086400>]
In [16]: _[0].name, _[0].value
Out[16]: ('Huono vakuutus', '0-vakuutus, mitään ei kata')
问题是:您可能不希望继承自 db.Model
的多个 类 作为基础,而是希望继承自 DocMapping
的 类。它作为层次结构更有意义。