我正在使用 SQLAlchemy 以编程方式查询具有复合外键的表。例如:
CREATE TABLE example (
id INT NOT NULL,
date TIMESTAMP NOT NULL,
data VARCHAR(128)
PRIMARY KEY (id, date)
)
我想获取一个值列表并取回行,例如:
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)
如果每一列都是独立的,我可以做
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)
)
但这显然不能只带来唯一匹配的 (id, date) 元组。我怀疑有一种方法可以指定要查询的复合主键,但搜索后我找不到任何文档。