Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
SQLAlchemy Setup (Response to "ANN: Pylons 0.9.4 Released")
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Expand all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
__wyatt  
View profile  
 More options Mar 23 2007, 10:37 pm
From: "__wyatt" <wyatt.lee.bald...@gmail.com>
Date: Sat, 24 Mar 2007 02:37:19 -0000
Local: Fri, Mar 23 2007 10:37 pm
Subject: SQLAlchemy Setup (Response to "ANN: Pylons 0.9.4 Released")
Note: I couldn't reply directly to the original message[1] in the
group, so I'm creating a new post.

[1]
http://groups.google.com/group/pylons-discuss/browse_frm/thread/1f05f...

On Dec 30 2006, 12:56 am, Ben Bangert <b...@groovie.org> wrote:

> On Dec 29, 2006, at 11:08 PM, Jose Galvez wrote:

> > Any chance we could get a short example showing how to use the new
> >sqlalchemyfeatures

> Sure, the two methods of interest are in pylons.database here:http://pylonshq.com/docs/0.9.4/module-pylons.database.html#create_eng...

> Since aSQLAlchemyengine is a pooled connection, only one of them
> needs to be created for your application. The create_engine stores
> the connection pool in your 'g' object.
>From my reading of the code, ``create_engine`` *doesn't* store the

connection pool in ``g`` but ``make_session`` does. Also, from what I
can tell from your example, you never use ``create_engine`` (the one
from pylons.database), and it's not clear where you create your
engine.

I think that's because when pylons.database is imported,
``pylons.database.session_context`` is automatically created and in
the process calls ``make_session``, which in turn calls
``create_engine``. That engine is accessible by doing something like
this:

    from pylons.database import session_context
    engine = session_context.current.bind_to

I do that in my model so I can call metadata.connect(model.engine) in
my ``BaseController`` (aside: Elixir's metadata currently requires
doing this).

It seems like (in a basic setup) all you have to do is import
pylons.database
at some point during app initialization (probably in your model) and
clear
``pylons.database.session_context.current`` on every request, using
one of
these methods:

    pylons.database.session_context.current = make_session()

    # This next one is suggested by pjenvey in this post:
    # http://groups.google.com/group/pylons-discuss/msg/0fb2b9f789b341ea
    # It has the advantage of not having to import
    # ``pylons.database.make_session``
    del pylons.database.session_context.current

    # May be slower than the two above
    pylons.database.session_context.current.clear()

You can import ``session_context`` in your ``models.__init__`` to make
things a little simpler. This is what my current setup looks like:

    # development.ini
    [app:main]
    # other stuff
    sqlalchemy.dburi = mysql://wyatt:password@localhost/wyatt_dev
    sqlalchemy.echo = true

    # models/__init__.py
    from pylons.database import session_context
    from elixir import metadata
    engine = session_context.current.bind_to
    class SomeEntity(Entity):
        has_field('title', String)
        # etc.

    # lib/base.py
    def __call__(self, environ, start_response):
        del model.session_context.current
        model.metadata.connect(model.engine)
        return WSGIController.__call__(self, environ, start_response)

And that's everything I have related to setting up SQLAlchemy for use
in my app. I struggled with this a bit, so I hope this helps somebody
(and I hope it really works!). Any corrections appreciated.

__wyatt


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
__wyatt  
View profile  
 More options Mar 23 2007, 10:41 pm
From: "__wyatt" <wyatt.lee.bald...@gmail.com>
Date: Sat, 24 Mar 2007 02:41:26 -0000
Local: Fri, Mar 23 2007 10:41 pm
Subject: Re: SQLAlchemy Setup (Response to "ANN: Pylons 0.9.4 Released")
Uh, for some reason, the first line of my response got pushed up so
that it looks like part of Ben's message. So, in case it's not clear,
my response starts at "From my reading of the code, ``create_engine``
*doesn't* store the...".

__wyatt

On Mar 23, 7:37 pm, "__wyatt" <wyatt.lee.bald...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Elixir has its own SessionContext" by __wyatt
__wyatt  
View profile  
 More options Mar 26 2007, 6:21 am
From: "__wyatt" <wyatt.lee.bald...@gmail.com>
Date: Mon, 26 Mar 2007 10:21:14 -0000
Local: Mon, Mar 26 2007 6:21 am
Subject: Elixir has its own SessionContext
On Mar 23, 7:37 pm, "__wyatt" <wyatt.lee.bald...@gmail.com> wrote:

Elixir has its own SessionContext that it uses with assign_mapper [1],
and what I wrote above doesn't seem to work very well with Elixir
(i.e., objects aren't refreshed). Here's what seems to be working now
(showing only the relevant parts):

>     # old models/__init__.py
>     from pylons.database import session_context
>     from elixir import metadata
>     engine = session_context.current.bind_to
>     class SomeEntity(Entity):
>         has_field('title', String)
>         # etc.

# new & improved models/__init__.py
from elixir import metadata, objectstore
session_context = objectstore.context
class SomeEntity(Entity):
    has_field('title', String)
    # etc.

>     # old lib/base.py
>     def __call__(self, environ, start_response):
>         del model.session_context.current
>         model.metadata.connect(model.engine)
>         return WSGIController.__call__(self, environ, start_response)

# new & improved lib/base.py
from pylons.database import make_session
def __call__(self, environ, start_response):
    db_session = make_session()
    session_context.current = db_session
    metadata.connect(db_session.bind_to)

__wyatt

[1] http://elixir.ematia.de/elixir/entity.py.html#165


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google