Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
New SQLAlchemy session created when referenced in views package
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 - Collapse 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
 
Danny Im  
View profile  
 More options Sep 5 2012, 4:44 pm
From: Danny Im <ior...@gmail.com>
Date: Wed, 5 Sep 2012 13:44:47 -0700 (PDT)
Local: Wed, Sep 5 2012 4:44 pm
Subject: New SQLAlchemy session created when referenced in views package

Hi,

I have an issue where if I import a sqlalchemy session and try issuing
queries with it I'll get this error:

UnboundExecutionError: Could not locate a bind configured on mapper
Mapper|MyModel|models, SQL expression or this Session

This only happens from within a package within the application, as I've put
all of my views related files into a python package in order to better
organize my views.

I've tested this with view functions that reside on the 'top level' app
directory (e.g. app/views.py) and any view functions within that file don't
raise the above exception.

Here are some code bits that I think are relevant:

In /app/models.py:
DBSession =
scoped_session(sessionmaker(extension=ZopeTransactionExtension(),
autoflush=False))
def initialize_sql(engine):
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)

In /app/views.py:
from models import DBSession, MyModel
session = DBSession()
session.query(MyModel)

In /app/view_dir/stuff.py:
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', '..', 'app')))
from models import DBSession, MyModel
session = DBSession()
session.query(MyModel)

I've searched for what might be causing the above error, and many results
suggest that the session was not bound to a database.  I tested this by
issuing print statements of the DBSession, and found that the DBSession
object is indeed different when it is imported to /app/view_dir/stuff.py,
and the engine bound to it is not set.

I've been able to reproduce this behavior by creating a new pyramid project
using the sqlalchemy scaffold, creating a package to store view files, and
creating view functions that issue queries via a sqlalchemy session object
imported from the models.py file.

Any ideas on why this is happening?  

Thanks!


 
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.
Jonathan Vanasco  
View profile  
 More options Sep 5 2012, 8:50 pm
From: Jonathan Vanasco <jonat...@findmeon.com>
Date: Wed, 5 Sep 2012 17:50:21 -0700 (PDT)
Local: Wed, Sep 5 2012 8:50 pm
Subject: Re: New SQLAlchemy session created when referenced in views package

scoped_session returns a session, so to me i see:

/app/models.py:
   creates DBSession instance via scoped_session
   initialize_sql initializes it

/app/views / etc :
   imports the DBSession from /app/models, then calls it?  i think that's
where an issue may be.

have you looked into the SqlAHelper package
? https://github.com/Pylons/SQLAHelper

it's in a freeze mode, and abandoned, but it still works.


 
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.
Michael Merickel  
View profile  
 More options Sep 5 2012, 8:53 pm
From: Michael Merickel <mmeri...@gmail.com>
Date: Wed, 5 Sep 2012 19:52:31 -0500
Local: Wed, Sep 5 2012 8:52 pm
Subject: Re: New SQLAlchemy session created when referenced in views package
It appears from your example code as if you're using the session from
module-scope. This is bad practice because you can't ensure that your
initialize_sql() function has executed before that code is run (when
the module is imported).


 
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 »