Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31825 > unrolled thread

SQLAlchemy: How to do Table Reflection and MySQL?

Started byNick Sabalausky <SeeWebsiteToContactMe@semitwist.com>
First post2012-10-20 19:24 -0400
Last post2012-10-23 19:15 -0400
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  SQLAlchemy: How to do Table Reflection and MySQL? Nick Sabalausky <SeeWebsiteToContactMe@semitwist.com> - 2012-10-20 19:24 -0400
    Re: SQLAlchemy: How to do Table Reflection and MySQL? darnold <darnold992000@yahoo.com> - 2012-10-22 14:35 -0700
      Re: SQLAlchemy: How to do Table Reflection and MySQL? Nick Sabalausky <SeeWebsiteToContactMe@semitwist.com> - 2012-10-23 18:28 -0400
        RE: SQLAlchemy: How to do Table Reflection and MySQL? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-23 22:42 +0000
          Re: SQLAlchemy: How to do Table Reflection and MySQL? Nick Sabalausky <SeeWebsiteToContactMe@semitwist.com> - 2012-10-23 19:15 -0400

#31825 — SQLAlchemy: How to do Table Reflection and MySQL?

FromNick Sabalausky <SeeWebsiteToContactMe@semitwist.com>
Date2012-10-20 19:24 -0400
SubjectSQLAlchemy: How to do Table Reflection and MySQL?
Message-ID<20121020192416.000023b6@unknown>
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?

[toc] | [next] | [standalone]


#31904

Fromdarnold <darnold992000@yahoo.com>
Date2012-10-22 14:35 -0700
Message-ID<26471ad3-2bcd-46ed-a697-ae768eb5fd3b@m4g2000yqf.googlegroups.com>
In reply to#31825
On Oct 20, 6:24 pm, Nick Sabalausky
<SeeWebsiteToContac...@semitwist.com> 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/2010/07/using-sqlalchemy-reflection-with-pylons.html

that came up from googling "sqlalchemy table reflection tutorial".

[toc] | [prev] | [next] | [standalone]


#31962

FromNick Sabalausky <SeeWebsiteToContactMe@semitwist.com>
Date2012-10-23 18:28 -0400
Message-ID<20121023182831.000038ae@unknown>
In reply to#31904
On Mon, 22 Oct 2012 14:35:23 -0700 (PDT)
darnold <darnold992000@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.)

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_7/core/tutorial.html

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

[toc] | [prev] | [next] | [standalone]


#31965

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-10-23 22:42 +0000
Message-ID<mailman.2696.1351032152.27098.python-list@python.org>
In reply to#31962
Nick Sabalausky wrote:
> On Mon, 22 Oct 2012 14:35:23 -0700 (PDT)
> darnold <darnold992000@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`. If you define 
the appropriate user it does it work? (From that page it seems 
like user should be a blank class like the following).

class user(object): pass

> 
> 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_7/core/tutorial.html
> 
> It said that but then didn't say how and didn't link to any info on how.

Ramit Prasad



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


#31969

FromNick Sabalausky <SeeWebsiteToContactMe@semitwist.com>
Date2012-10-23 19:15 -0400
Message-ID<20121023191516.000021a2@unknown>
In reply to#31965
On Tue, 23 Oct 2012 22:42:08 +0000
"Prasad, Ramit" <ramit.prasad@jpmorgan.com> wrote:

> Nick Sabalausky wrote:
> > On Mon, 22 Oct 2012 14:35:23 -0700 (PDT)
> > darnold <darnold992000@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web