Skip to content

Instantly share code, notes, and snippets.

@aodag
Created August 13, 2015 09:41
Show Gist options
  • Save aodag/043da0cc1a9c822abd81 to your computer and use it in GitHub Desktop.
Save aodag/043da0cc1a9c822abd81 to your computer and use it in GitHub Desktop.
>>> from sqlalchemy.sql.expression import table, column
>>> from sqlalchemy.sql.expression import select
>>> from sqlalchemy.sql.types import VARCHAR
>>> from sqlalchemy.dialects.postgresql import dialect
>>> employee = table('employee', column('id', VARCHAR), column('job_id', VARCHAR))
>>> job = table('job', column('id', VARCHAR), column('name', VARCHAR))
>>> str(select([employee.join(job, employee.c.job_id == job.c.id)]).compile(dialect=dialect()))
'SELECT employee.id, employee.job_id, job.id, job.name \nFROM employee JOIN job ON employee.job_id = job.id'
>>> stmt = select([employee.join(job, employee.c.job_id == job.c.id)]).where(job.c.name == 'programmer')
>>> str(stmt.compile(dialect=dialect()))
'SELECT employee.id, employee.job_id, job.id, job.name \nFROM employee JOIN job ON employee.job_id = job.id \nWHERE job.name = %(name_1)s'
>>> stmt.compile(dialect=dialect()).params
{u'name_1': 'programmer'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment