Querying with sqlalchemy

July 18, 2017

Context Still having some trouble wrapping my head around sqlalchemy’s orm, putting things into postgres and then querying it. The flow isn’t clear to me yet.

Querying ΒΆ

# Automaps the database here.
d = sps.Database()

# Tables are available in the classes property
Inventory = d.base.classes.inventory

# Creates a list of sqlalchemy object with all the rows (that's how my query_all method is implemented)
data = d.query_all(Inventory)

# I can access each row with this.
first_color = data[0].product

first_color

> u'Alaska Cream 3cm'

This should work for now. Took me a good 8 hours to understand this. SQLalchemy is hard to grok for me.

I'll tell you when I post stuff.

Subscribe to get my latest posts by email.