Storm中的多对一属性

时间:2010-07-20 22:44:23

标签: python storm-orm

我的架构看起来像这样:

CREATE TABLE plans (
    id SERIAL PRIMARY KEY,
    description text
);

CREATE TABLE projects (
    id SERIAL PRIMARY KEY,
    project_id character varying(240) UNIQUE,
    plan_id integer REFERENCES plans(id) ON DELETE CASCADE
);

我想按照

的方式进行Storm查询
plan = store.find(Plan, Plan.project_id == "alpha")
# should translate to something like
# SELECT p.* from plans p LEFT JOIN projects proj ON p.id = proj.plan_id
#     WHERE proj.project_id = 'alpha';

(请注意projects.plan_id并非唯一。)

如何设置?

1 个答案:

答案 0 :(得分:2)

对于给定的SQL,没有太多理由使用左连接,因为where子句不匹配任何没有相应项目的行。您可以通过以下方式获得结果:

result = store.find(Plan, Plan.id == Project.plan_id, Project.project_id == "alpha")

这将为您提供ResultSet对象。鉴于您的架构,看起来您期望单行,因此您可以使用以下命令访问:

plan = result.one()

或者将它们绑在一起:

plan = store.find(Plan, Plan.id == Project.plan_id, Project.project_id == "alpha").one()

如果你真的需要进行左连接,那么语法就是这样的:

result = store.using(LeftJoin(Plan, Project, Plan.id == Project.plan_id)).find(
    Plan, Project.project_id == "alpha")