從下面的代碼過濾器,我定義的關係是這樣的:SQLAlchemy的和關係
UnitTypeRowModel (self-referencing but only one in the doc)
-- PlanPmDefinition (definition in the json doc)
-- PlanPmValues (value in the json doc)
目前JSON作爲結果,如果我只有在數據庫表中的一個plan_ou:
[{
"children": [],
"parent_id": 1,
"id": 4
}, {
"children": [],
"parent_id": 2,
"plan_pm_id": 2,
"id": 5
"definition": {
"id": 2,
"tag": 0,
"value": {
"plan_pm_id": 2,
"tag": 0,
"plan_ou": 1
}
}
}]
代碼:
class PlanPmValues(Base):
__tablename__ = 'plan_pm_municipal_values'
plan_pm_id = Column(Integer, primary_key=True)
tag = Column(Integer, primary_key=True)
plan_ou = Column(Integer) # Filter on this
...
class PlanPmDefinition(Base):
__tablename__ = 'plan_pm_municipal_definition'
id = Column(Integer, primary_key=True)
tag = Column(Integer, primary_key=True) -- always 0
value = relationship(PlanPmValues,
primaryjoin='and_(PlanPmDefinition.id==PlanPmValues.plan_pm_id, ' +
'PlanPmDefinition.tag==PlanPmValues.tag)',
foreign_keys='[PlanPmValues.plan_pm_id, PlanPmValues.tag]',
lazy='joined', uselist=False)
class UnitTypeRowModel(Base):
__tablename__ = 'unit_type_row_model'
__table_args__ = {'schema': base_schema}
id = Column(Integer, primary_key=True)
client_id = Column(Integer, ForeignKey(base_schema + '.client.id'))
parent_id = Column(Integer, ForeignKey(base_schema + '.unit_type_row_model.id'), nullable=True)
plan_pm_id = Column(Integer, nullable=True)
children = relationship(
'UnitTypeRowModel',
lazy='joined',
join_depth=2,
order_by="UnitTypeRowModel.sort_order")
definition = relationship(
'PlanPmDefinition',
primaryjoin='and_(PlanPmDefinition.id==UnitTypeRowModel.plan_pm_id, ' +
'PlanPmDefinition.tag==0)',
foreign_keys='[UnitTypeRowModel.plan_pm_id]',
lazy='joined',
uselist=False)
@staticmethod
def get_for_unit(client_id, unit_id):
db_session = DatabaseEngine.get_session()
row_models = db_session.query(UnitTypeRowModel).\
filter(UnitTypeRowModel.client_id == client_id).\
order_by(UnitTypeRowModel.sort_order)
json = util.Serialize.serialize_to_json(row_models)
db_session.close()
return json
如何在方法UnitTypeRowModel.get_for_unit
中從PlanPmValues
中的plan_ou
過濾?
它應該是一對一的。我應該省略'tag'或將它添加到連接中,因爲它實際上始終爲0並且未被使用(儘管我不這麼做)。我已經更新了這個問題,並不是多對一(我希望)。我也不想要一個子查詢,而是一個直的左外連接,其中plan_ou必須在連接子句中設置,而不是在底部。我會再看看'association_proxy'。 – Asken
爲了在沒有子查詢的情況下獲得直接連接,可以將'UnitTypeRowModel'映射到3表連接。事實上,每當您查詢「UnitTypeRowModel」時,您就已經在進行這種連接,因爲您的關係中存在「lazy = joined」關鍵字。如果你想從'UnitTypeRowModel'類強制執行只讀訪問,我想你必須通過python屬性來運行它們以防止輕鬆書寫。讓我知道如果你想看到任何代碼,我會更新答案。 –
是的,我得到它所有我想要的,但我想要的是在查詢時從PlanPmValues-join在plan_ou上添加過濾器。 – Asken