Skip to content

Commit

Permalink
Clarify SQL join column in queries
Browse files Browse the repository at this point in the history
In particular, with SQLAlchemy 1.2.14, the query
`session.query(NCExperiment.experiment, ...).join(NCExperiment)`
fails because it cannot figure how to construct the join. We should be
explicit about the column over which we're joining, to be sure they're
executed correctly.
  • Loading branch information
angus-g committed Aug 19, 2019
1 parent 3253acc commit b74cedb
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions cosima_cookbook/querying.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def get_experiments(session):
"""

q = (session
.query(NCExperiment.experiment,
.query(NCExperiment.experiment,
func.count(NCFile.experiment_id).label('ncfiles'))
.group_by(NCFile.experiment_id)
.join(NCExperiment))
.join(NCFile.experiment)
.group_by(NCFile.experiment_id))

return pd.DataFrame(q)

Expand All @@ -36,8 +36,8 @@ def get_ncfiles(session, experiment):

q = (session
.query(NCFile.ncfile, NCFile.index_time)
.join(NCExperiment)
.filter(NCExperiment.experiment==experiment))
.join(NCFile.experiment)
.filter(NCExperiment.experiment == experiment))

return pd.DataFrame(q)

Expand All @@ -49,18 +49,18 @@ def get_variables(session, experiment, frequency=None):

q = (session
.query(CFVariable.name,
NCFile.frequency,
NCFile.frequency,
NCFile.ncfile,
func.count(NCFile.ncfile).label('# ncfiles'),
func.min(NCFile.time_start).label('time_start'),
func.max(NCFile.time_end).label('time_end'))
.join(NCFile.experiment)
.join(NCFile.ncvars)
.join(NCVar.variable)
.filter(NCExperiment.experiment==experiment)
.order_by(NCFile.frequency,
CFVariable.name,
NCFile.time_start,
.filter(NCExperiment.experiment == experiment)
.order_by(NCFile.frequency,
CFVariable.name,
NCFile.time_start,
NCFile.ncfile)
.group_by(CFVariable.name, NCFile.frequency))

Expand All @@ -82,8 +82,8 @@ def get_frequencies(session, experiment=None):
else:
q = (session
.query(NCFile.frequency)
.join(NCExperiment)
.filter(NCExperiment.experiment==experiment)
.join(NCFile.experiment)
.filter(NCExperiment.experiment == experiment)
.group_by(NCFile.frequency))

return pd.DataFrame(q)
Expand Down

0 comments on commit b74cedb

Please sign in to comment.