鉴于以下两个ORM类:
class Indi(conn.pampas.Base):
'SQLAlchemy class defining the pampas indi table.'
__tablename__ = 'indi'
__table_args__ = {'keep_existing': True}
__module__ = 'pampas.sql.tables.pampas'
cid_indicator_key = sqlalchemy.Column(sqlalchemy.String(200), primary_key=True, nullable=False, doc='col_doc, primary_key=True, nullable=False)')
cfl_active = sqlalchemy.Column(sqlalchemy.String(1), nullable=False, doc='Column(String, nullable=False)')
ctx_desc = sqlalchemy.Column(sqlalchemy.String(200), nullable=False, doc='Column(String, nullable=False)')
cfl_indicator_type = sqlalchemy.Column(sqlalchemy.String(1), nullable=False, doc='Column(String, nullable=False)')
nno_timeout = sqlalchemy.Column(sqlalchemy.Integer(), nullable=False, doc='Column(Integer, nullable=False)')
ctx_tooltip = sqlalchemy.Column(sqlalchemy.String(500), doc='Column(String, nullable=False)')
ctx_hyperlink = sqlalchemy.Column(sqlalchemy.String(2000), doc='Column(String, nullable=False)')
def __repr__(self):
'Returns repr(self).'
fmt_str = "<{cls}({cid_indicator_key}={cid_indicator_key!r}, {cfl_active}={cfl_active!r}, {ctx_desc}={ctx_desc!r}, {cfl_indicator_type}={cfl_indicator_type!r}, {nno_timeout}={
nno_timeout!r}, {ctx_tooltip}={ctx_tooltip!r}, {ctx_hyperlink}={ctx_hyperlink!r})>"
params = {
'cls': type(self).__name__,
'cid_indicator_key': self.cid_indicator_key,
'cfl_active': self.cfl_active,
'ctx_desc': self.ctx_desc,
'cfl_indicator_type': self.cfl_indicator_type,
'nno_timeout': self.nno_timeout,
'ctx_tooltip': self.ctx_tooltip,
'ctx_hyperlink': self.ctx_hyperlink,
}
return fmt_str.format(**params)
class IndiHierarchy(conn.pampas.Base):
'SQLAlchemy class defining the pampas indi_hierarchy table.'
__tablename__ = 'indi_hierarchy'
__table_args__ = {'keep_existing': True}
__module__ = 'pampas.sql.tables.pampas'
cid_parent_indicator_key = sqlalchemy.Column(sqlalchemy.String(200), sqlalchemy.ForeignKey('indi.cid_indicator_key'), primary_key=True, nullable=False, doc="col_doc, primary_key=T
rue, nullable=False, foreign_key='indi.cid_indicator_key')")
cid_child_indicator_key = sqlalchemy.Column(sqlalchemy.String(200), sqlalchemy.ForeignKey('indi.cid_indicator_key'), primary_key=True, nullable=False, doc="col_doc, primary_key=Tr
ue, nullable=False, foreign_key='indi.cid_indicator_key')")
nno_seq_num = sqlalchemy.Column(sqlalchemy.Integer(), nullable=False, doc='Column(Integer, nullable=False)')
cid_layout_id = sqlalchemy.Column(sqlalchemy.String(200), doc='Column(String, nullable=False)')
ctx_url_params = sqlalchemy.Column(sqlalchemy.String(100), doc='Column(String, nullable=False)')
cid_plugin_id = sqlalchemy.Column(sqlalchemy.String(200), sqlalchemy.ForeignKey('plugin.plugin_id'), doc="Column(String, nullable=False, foreign_key='plugin.plugin_id')")
ctx_created_by = sqlalchemy.Column(sqlalchemy.String(1), nullable=False, doc='Column(String, nullable=False)')
cid_plant = sqlalchemy.Column(sqlalchemy.String(10), doc='Column(String, nullable=False)')
cid_version = sqlalchemy.Column(sqlalchemy.Float(), nullable=False, server_default=sqlalchemy.schema.FetchedValue(), doc='Column(Float, nullable=False, server_default=FetchedValue
)')
indi_child = sqlalchemy.orm.relationship(("'Indi'",), backref=sqlalchemy.orm.backref('indi_hierarchy_child'), primaryjoin='IndiHierarchy.cid_child_indicator_key == Indi.cid_indica
tor_key')
indi_parent = sqlalchemy.orm.relationship(("'Indi'",), backref=sqlalchemy.orm.backref('indi_hierarchy_parent'), primaryjoin='IndiHierarchy.cid_parent_indicator_key == Indi.cid_ind
icator_key')
plugin = sqlalchemy.orm.relationship(("'Plugin'",), backref=sqlalchemy.orm.backref('indi_hierarchy'))
def __repr__(self):
'Returns repr(self).'
fmt_str = "<{cls}({cid_parent_indicator_key}={cid_parent_indicator_key!r}, {cid_child_indicator_key}={cid_child_indicator_key!r}, {nno_seq_num}={nno_seq_num!r}, {cid_layout_id
}={cid_layout_id!r}, {ctx_url_params}={ctx_url_params!r}, {cid_plugin_id}={cid_plugin_id!r}, {ctx_created_by}={ctx_created_by!r}, {cid_plant}={cid_plant!r}, {cid_version}={cid_version
!r})>"
params = {
'cls': type(self).__name__,
'cid_parent_indicator_key': self.cid_parent_indicator_key,
'cid_child_indicator_key': self.cid_child_indicator_key,
'nno_seq_num': self.nno_seq_num,
'cid_layout_id': self.cid_layout_id,
'ctx_url_params': self.ctx_url_params,
'cid_plugin_id': self.cid_plugin_id,
'ctx_created_by': self.ctx_created_by,
'cid_plant': self.cid_plant,
'cid_version': self.cid_version,
}
return fmt_str.format(**params)
我尝试以下查询:
indi_upd_keys = ('W10_IPST_SERVERHEALTH_LOAD',)
with conn.pampas.session() as session:
query2 = session.query(tables.pampas.Indi).filter(tables.pampas.Indi.cid_indicator_key.in_(indi_upd_keys))
print(query2)
print(session.execute(query2))
这会导致以下错误:
ArgumentError:关系'indi_child'需要一个类或一个mapper参数(收到:&lt; class'tuple'&gt;)
这似乎是由于我使用in_()
方法造成的。但是,我对in_()
的使用不应该导致这种情况,对吗?
有没有更好的方法来完成我需要的东西?我需要能够匹配cid_indicator_key
字段的多个潜在值。 in_()
运算符对此似乎是正确的。我错了吗?
或者我应该以不同的方式定义我的关系?我知道使用backref
有特性。
答案 0 :(得分:0)
我觉得愚蠢,我看到了问题。
indi_child = sqlalchemy.orm.relationship(("'Indi'",), backref=sqlalchemy.orm.backref('indi_hierarchy_child'), primaryjoin='IndiHierarchy.cid_child_indicator_key == Indi.cid_indica
tor_key')
indi_parent = sqlalchemy.orm.relationship(("'Indi'",), backref=sqlalchemy.orm.backref('indi_hierarchy_parent'), primaryjoin='IndiHierarchy.cid_parent_indicator_key == Indi.cid_ind
icator_key')
plugin = sqlalchemy.orm.relationship(("'Plugin'",), backref=sqlalchemy.orm.backref('indi_hierarchy'))
应该是:
indi_child = sqlalchemy.orm.relationship('Indi', backref=sqlalchemy.orm.backref('indi_hierarchy_child'), primaryjoin='IndiHierarchy.cid_child_indicator_key == Indi.cid_indica
tor_key')
indi_parent = sqlalchemy.orm.relationship('Indi', backref=sqlalchemy.orm.backref('indi_hierarchy_parent'), primaryjoin='IndiHierarchy.cid_parent_indicator_key == Indi.cid_ind
icator_key')
plugin = sqlalchemy.orm.relationship('Plugin', backref=sqlalchemy.orm.backref('indi_hierarchy'))