Skip to content

Instantly share code, notes, and snippets.

@Stiivi
Created April 27, 2012 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stiivi/2506388 to your computer and use it in GitHub Desktop.
Save Stiivi/2506388 to your computer and use it in GitHub Desktop.
SQLAlchemy bug
from sqlalchemy import *
from sqlalchemy import sql
def create_table(url, schema=None):
engine = create_engine(url)
metadata = MetaData(engine, reflect=True)
table = Table("test_table", metadata, schema=schema)
if table.exists():
table.drop()
table = Table("test_table", metadata, schema=schema)
table.append_column(Column("name", String))
table.create()
table.insert(("foo", )).execute()
def get_key(url,schema=None):
print "URL : %s" % url
print "schema : %s" % schema
create_table(url, schema)
engine = create_engine(url)
metadata = MetaData(engine, reflect=True)
table = Table("test_table", metadata, schema=schema, autoload=True)
if not table.exists():
print "table does not exist"
print "table : %s" % (table, )
print "columns : %s" % (table.columns, )
column = table.c["name"].label("foo.bar")
cursor = sql.expression.select([column])
row = cursor.execute().fetchone()
print "keys are: %s" % (row.keys(), )
print ""
# Each should print keys as "foo.bar"
get_key("postgres://localhost/sandbox")
get_key("postgres://localhost/sandbox", "test")
get_key("sqlite:////tmp/test.sqlite")
# The sqlite prints just "bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment