Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > SQLAlchemy: How to do Table Reflection and MySQL?

Reply
Thread Tools

SQLAlchemy: How to do Table Reflection and MySQL?

 
 
Nick Sabalausky
Guest
Posts: n/a
 
      10-20-2012
Hi, I'm fairly new to Python, and I'm trying to figure out how to use
SQLAlchemy to connect to a MySQL DB and use table reflection to set up
SQLAlchemy's tables. But the SQLAlchemy documentation is gigantic and
frankly kinda making my head spin, so I'm having trouble even finding
any information on how to use its table reflection, mostly just that it
exists and *can* be done, but not so much "how". My web searching has
just been turning up examples of SQLite and manually describing the
tables in Python and having SQLAlchemy create the tables, which isn't
what I'm looking for.

Is there a simple way to do this somehow? To just connect to a MySQL DB
and use table reflection?
 
Reply With Quote
 
 
 
 
darnold
Guest
Posts: n/a
 
      10-22-2012
On Oct 20, 6:24*pm, Nick Sabalausky
<(E-Mail Removed)> wrote:
> Hi, I'm fairly new to Python, and I'm trying to figure out how to use
> SQLAlchemy to connect to a MySQL DB and use table reflection to set up
> SQLAlchemy's tables. But the *SQLAlchemy documentation is gigantic and
> frankly kinda making my head spin, so I'm having trouble even finding
> any information on how to use its table reflection, mostly just that it
> exists and *can* be done, but not so much "how". My web searching has
> just been turning up examples of SQLite and manually describing the
> tables in Python and having SQLAlchemy create the tables, which isn't
> what I'm looking for.
>
> Is there a simple way to do this somehow? To just connect to a MySQL DB
> and use table reflection?


i'm not brave enough to dig too deeply into SQLAlchemy, but maybe this
will help? :

http://kashififtikhar.blogspot.com/2...th-pylons.html

that came up from googling "sqlalchemy table reflection tutorial".
 
Reply With Quote
 
 
 
 
Nick Sabalausky
Guest
Posts: n/a
 
      10-23-2012
On Mon, 22 Oct 2012 14:35:23 -0700 (PDT)
darnold <(E-Mail Removed)> wrote:
>
> i'm not brave enough to dig too deeply into SQLAlchemy, but maybe this
> will help? :
>
> http://kashififtikhar.blogspot.com/2...th-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").firs t()
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.)

I also tried this which also fails:

res =
session.query(meta.tables["user"]).filter(meta.tables["user"].name=="bert").first()

sqlalchemy.exc.ArgumentError: filter() argument must be of type
sqlalchemy.sql.ClauseElement or string

The page you linked to appears to get around the matter by manually
setting up tables filled with the reflected info, but that seems to
defeat much of the point for me. I may as well just set up the tables
manually without the reflection, which is what I'll probably do.

Maybe I just misunderstood what was meant in the SQLAlchemy docs here?:

"but note that SA can also “import” whole sets of Table objects
automatically from an existing database (this process is called table
reflection)." --
http://docs.sqlalchemy.org/en/rel_0_.../tutorial.html

It said that but then didn't say how and didn't link to any info on how.

 
Reply With Quote
 
Nick Sabalausky
Guest
Posts: n/a
 
      10-23-2012
On Tue, 23 Oct 2012 22:42:08 +0000
"Prasad, Ramit" <(E-Mail Removed)> wrote:

> Nick Sabalausky wrote:
> > On Mon, 22 Oct 2012 14:35:23 -0700 (PDT)
> > darnold <(E-Mail Removed)> wrote:
> > >
> > > i'm not brave enough to dig too deeply into SQLAlchemy, but maybe
> > > this will help? :
> > >
> > > http://kashififtikhar.blogspot.com/2...th-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").firs t()
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Table/table rows/table data tag question? Rio HTML 4 11-05-2004 08:11 AM
Reflection and COM Alan Seunarayan ASP .Net 3 01-14-2004 10:56 PM
Could not load type VTFixup Table from assembly Invalid token in v-table fix-up table. David Williams ASP .Net 2 08-12-2003 07:55 AM
Re: Reflection and SqlConnection WebDevHokie ASP .Net 0 07-10-2003 06:33 PM



Advertisments