Note: this is a question about Alchemy's SQL expression language, not ORM
SQL Alchemy is great for adding WHERE or HAVING clauses to an existing query:
q = select([bmt_gene.c.id]).select_from(bmt_gene)
q = q.where(bmt_gene.c.ensembl_id == "ENSG00000000457")
print q
SELECT bmt_gene.id
FROM bmt_gene
WHERE bmt_gene.ensembl_id = %s
However, if you try to add a JOIN in the same way, you will get an exception:
q = select([bmt_gene.c.id]).select_from(bmt_gene)
q = q.join(bmt_gene_name)
sqlalchemy.exc.NoForeignKeysError: cannot find any foreign key relationship between 'Select object' and 'bmt_gene_name'
If you specify columns, it creates a subquery (which is still incomplete SQL):
q = select([bmt_gene.c.id]).select_from(bmt_gene)
q = q.join(bmt_gene_name, q.c.id == bmt_gene_name.c.gene_id)
(SELECT bmt_gene.id AS id FROM bmt_gene)
JOIN bmt_gene_name ON id = bmt_gene_name.gene_id
But I really want:
SELECT
bmt_gene.id AS id
FROM
bmt_gene
JOIN bmt_gene_name ON id = bmt_gene_name.gene_id
edit: adding a JOIN should be after creating the original q expression. The idea is that I create a basic request skeleton, then iterate over all the connections requested by the user and add them to the request.
SQL Alchemy?