tags:

views:

23

answers:

1

My schema looks something like this:

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
);

And I want to do Storm queries along the lines of

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';

(Note that projects.plan_id is not unique.)

How do I set this up?

+1  A: 

For the given SQL, there isn't much reason to use a left join, since your where clause won't match any rows where there isn't a corresponding project. You could get the results with:

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

This will give you a ResultSet object. Given your schema, it looks like you're expecting a single row, so you can access that with:

plan = result.one()

Or tie them both together with:

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

If you really need to do a left join, the syntax for that would be something like this:

result = store.using(LeftJoin(Plan, Project, Plan.id == Project.plan_id)).find(
    Plan, Project.project_id == "alpha")
James Henstridge
Looks good. I guess I was expecting to be able to define the Plan.id == Project.plan_id relationship in some attribute on Plan, and not explicitly in the find expression, but that works.
keturn
Well, depending on how you structure your code, adding Reference or ReferenceSet properties to your classes might fit your needs better.For instance, if you had already retrieved the relevant Project row, you could access the plan as "project.plan" if you defined a reference on the Project class as: plan = Reference(plan_id, Plan.id)But if I was starting with the project name and wanted the plan directly, then the single find() statement is going to result in less queries.
James Henstridge