SQLAlchemy: How to do Table Reflection and MySQL?

Nick Sabalausky SeeWebsiteToContactMe at semitwist.com
Tue Oct 23 19:15:16 EDT 2012


On Tue, 23 Oct 2012 22:42:08 +0000
"Prasad, Ramit" <ramit.prasad at jpmorgan.com> wrote:

> Nick Sabalausky wrote:
> > On Mon, 22 Oct 2012 14:35:23 -0700 (PDT)
> > darnold <darnold992000 at yahoo.com> wrote:
> > >
> > > i'm not brave enough to dig too deeply into SQLAlchemy, but maybe
> > > this will help? :
> > >
> > > http://kashififtikhar.blogspot.com/2010/07/using-sqlalchemy-reflection-with-pylons.html
> > >
> > > that came up from googling "sqlalchemy table reflection tutorial".
> > 
> > Thanks, your view of Google seems to be far better tailored for
> > Python than mine is, that doesn't come up for me anywhere on the
> > first five pages of results for that query.
> > 
> > Unfortunately the info on that page doesn't seem to work for me:
> > 
> > ----------------------------------
> > from sqlalchemy import *
> > from sqlalchemy.orm import sessionmaker
> > 
> > engine = create_engine(my connection string)
> > meta = MetaData()
> > meta.bind = engine
> > meta.reflect()
> > 
> > Session = sessionmaker(bind=engine)
> > session = Session()
> > 
> > res = session.query(user).filter(user.name=="bert").first()
> > print res.name
> > ----------------------------------
> > 
> > That just gives me:
> > 
> > NameError: name 'user' is not defined
> > 
> > (And yes, the code given on that page to print out the table info
> > *does* indicate a table named 'user' was found.)
> 
> This does not seem to be a SQLAlchemy problem. Instead it seems
> there is not a variable called `name`. 

Oops, yea, it's supposed to be:

    meta.tables["user"].columns["name"]

Not:

    meta.tables["user"].name

Works now, thanks all.




More information about the Python-list mailing list