I'm using SQLAlchemy to programmatically query a table with a composite foreign key. e.g.:
CREATE TABLE example (
id INT NOT NULL,
date TIMESTAMP NOT NULL,
data VARCHAR(128)
PRIMARY KEY (id, date)
)
I'd like to take a list of values and get rows back e.g.:
interesting_data = (
(1, '2016-5-1'),
(1, '2016-6-1'),
(2, '2016-6-1'),
(3, '2016-5-1'),
(3, '2016-6-1'),
)
select(
[example.c.id, example.c.date, example.c.data],
).where(example.primary_key.in_(interesting_data)
If each column was independent I could do
interesting_ids = [1,2,3]
interesting_dates = ['2016-5-1', '2016-6-1']
select(
[example.c.id, example.c.date, example.c.data],
).where(
example.c.id.in_(interesting_ids)
).where(
example.c.date.in_(interesting_dates)
)
But that clearly fails to bring only the unique matching (id, date) tuples. I suspect there is a way to specify the compound primary key to query, but I can't find any documentation after a search.
Use a list comprehension in your where clause:
However, a separate issue I noticed is that you're comparing a date column to string data type. The
interesting_datalist should beAlso, note that it is possible to create a basic statement, and then add clauses to it incrementally, leading to (hopefully) better legibility and code reuse.
So, it would be possible to write the above as
This generates the following sql (new-lines & spaces added by me):
note: if you have a lot of rows to filter like this, it may be more efficient to create a temporary table, insert rows into this temporary table from
interesting_data, and then inner join to this table, rather than add the where clause as shown above.