Could someone show me how I can accomplish this in sqlalchemy?
SELECT
*
FROM animals a
INNER JOIN species s
ON (s.species_id=a.id AND s.type='mammals')
LIMIT 1;
I have tried many different things, but I keep getting weird results. If someone please help me with this I would greatly appreciate this.
Some code I have done:
result = session.query(Animal).\
join((Species, (Species.species_id==Animal.id)), (Species, (Species.type=='mammals')))
.all()
This of course gives me:
SELECT
*
FROM animals
INNER JOIN species
ON (species.species_id=animals.id)
INNER JOIN species
ON (AND species.type='mammals')
LIMIT 1;
But this is not exactly what I want.
Thanks in advance.
-e
You just need and_
:
from sqlalchemy.sql import and_
result = session.query(Animal).join(Species, and_(
Species.species_id == Animal.id,
Species.type == 'mammals',
)).all()
EDIT: this is using the "two-argument calling form" of join
available in SQLAlchemy 0.7 and later.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.