Re: [sqlalchemy] Is Engine instance object threadsafe?

2011-03-30 Thread Michael Bayer

On Mar 30, 2011, at 6:48 PM, Ajay wrote:

> We use sqlalchemy 0.4.6 with elixir 0.5.2 and zope.
> 
> Below is some excerpt to show how we currently create/manage one
> engine instance per zope thread.
> 
> From what I understand from reading the documentation, the
> 'connection' and 'transaction' objects themselves are not thread safe
> - but the engine instance when bound to metadata can manage
> connections from a pool and as long as we 'close' (release back to
> pool) within each thread then it may be safe.
> 
> We want to try to limit our application from consu,ing too many
> database conections.
> So can somebody confirm whether it's prudent to change our approach
> and share the engine instance among multiple threads?

The engine is absolutely threadsafe.   It has no state other than its reference 
to the pool, which is designed to work in a threaded environment (that said, I 
couldn't recall what issues have been fixed in the pool since 0.4.6, which was 
three years ago, but there have been several).   There is no need to store 
engines per threads or to use the ThreadLocalMetaData object for pretty much 
anything.   TLMD is built for the almost-never use case of an application that 
wants to use bound metadata (which is already a use case I've been 
de-emphasizing for three years) with a different DSN per thread, like a web 
application where each request deals with a different database.   

If anything the approach you have below will guarantee that the app would use 
as many connections as there are threads in the application, even when those 
threads are dormant.



> 
> thanks
> Aj
> 
> 
> +++
> # global declarations
> __session__ =
> scoped_session(sessionmaker(twophase=False,transactional=True,autoflush=True))
> __metadata__ = ThreadLocalMetaData()
> 
> _tld = threading.local()
> 
> # Manage one engine instance per thread
> if not hasattr(thread_local_data,'txm_orm_engine_dict'):
># THREAD LOCAL ATTR DOESN'T EXIST. CREATE IT.
>thread_local_data.txm_orm_engine_dict = dict()
>engine = sqlalchemy.create_engine(dsn,echo=True)
> 
># UPDATE DICTIONARY
>thread_local_data.txm_orm_engine_dict[dsn] = engine
> else:
># ALREADY THREAD LOCAL ENGINE DICTIONARY.
>if thread_local_data.txm_orm_engine_dict.has_key(dsn):
># ALREADY AN ENGINE FOR THIS DSN. USE IT
>engine =
> thread_local_data.txm_orm_engine_dict[dsn]
> 
>else:
># NO ENGINE FOR THIS DSN. CREATE IT
>engine =
> sqlalchemy.create_engine(dsn,echo=True)
># UPDATE DICTIONARY
>thread_local_data.txm_orm_engine_dict[dsn] =
> engine
> 
> 
> __metadata__.bind = engine
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Is Engine instance object threadsafe?

2011-03-30 Thread Ajay
We use sqlalchemy 0.4.6 with elixir 0.5.2 and zope.

Below is some excerpt to show how we currently create/manage one
engine instance per zope thread.

>From what I understand from reading the documentation, the
'connection' and 'transaction' objects themselves are not thread safe
- but the engine instance when bound to metadata can manage
connections from a pool and as long as we 'close' (release back to
pool) within each thread then it may be safe.

We want to try to limit our application from consu,ing too many
database conections.
So can somebody confirm whether it's prudent to change our approach
and share the engine instance among multiple threads?

thanks
Aj


+++
# global declarations
__session__ =
scoped_session(sessionmaker(twophase=False,transactional=True,autoflush=True))
__metadata__ = ThreadLocalMetaData()

_tld = threading.local()

# Manage one engine instance per thread
if not hasattr(thread_local_data,'txm_orm_engine_dict'):
# THREAD LOCAL ATTR DOESN'T EXIST. CREATE IT.
thread_local_data.txm_orm_engine_dict = dict()
engine = sqlalchemy.create_engine(dsn,echo=True)

# UPDATE DICTIONARY
thread_local_data.txm_orm_engine_dict[dsn] = engine
else:
# ALREADY THREAD LOCAL ENGINE DICTIONARY.
if thread_local_data.txm_orm_engine_dict.has_key(dsn):
# ALREADY AN ENGINE FOR THIS DSN. USE IT
engine =
thread_local_data.txm_orm_engine_dict[dsn]

else:
# NO ENGINE FOR THIS DSN. CREATE IT
engine =
sqlalchemy.create_engine(dsn,echo=True)
# UPDATE DICTIONARY
thread_local_data.txm_orm_engine_dict[dsn] =
engine


__metadata__.bind = engine


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Self-referencing table without Foreign-Key SQLAlchemy

2011-03-30 Thread Michael Bayer

On Mar 30, 2011, at 2:58 PM, Mene wrote:

> Hello, often when you have a table with selfrefernece, say in a tree,
> you have an attribute as a foreign key, which is pointing to the
> primary key of the table. (like in the Adjacency List Relationships
> example from the docs)
> 
> However, I have a natural key, which works like this:
> "" is the root
> "a", "b", "c" etc. are the children of ""
> "aa", "ab", "ac" etc. are the children of "a"
> "ba", "bb", "bc" etc. are the children of "b"
> "aaa", "aab", "aac" etc. are the children of "aa"
> 
> So there is one character for each level in the tree and the child
> nodes of each node are those with the same beginning and one character
> added.
> 
> How would I do this using an SQLAlchemy mapper without adding an extra
> attribute to reference the parent?
> 
> Note: I'm only interested in reading the relation ship, if something
> like node.children.append(child) isn't working thats fine. Also I'm
> stuck to version 0.4.8, however if this is not possible in this
> version but only in a newer one I might take the effort to update.

this is called "materialized path" in SQL parlance and you'd need to forego the 
usage of relation() in favor of a descriptor that loads the records you want:

class MyNode(object):
@property
def children(self):
return 
object_session(self).query(MyNode).filter(MyNode.key.like(self.key + "?")).all()

there are also strategies by which you can use MapperExtension to intercept the 
point at which rows are appended to the result list, and instead build the 
"child" structure at that point.   There is an old example (but you're on 0.4 
anyway) in the 0.4 dist called examples/adjacencytree/byroot_tree.py which does 
this.

> 
> Also on stackoverflow, if you prefer:
> http://stackoverflow.com/questions/5471687/self-referencing-table-without-foreign-key-sqlalchemy
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Self-referencing table without Foreign-Key SQLAlchemy

2011-03-30 Thread Mene
Hello, often when you have a table with selfrefernece, say in a tree,
you have an attribute as a foreign key, which is pointing to the
primary key of the table. (like in the Adjacency List Relationships
example from the docs)

However, I have a natural key, which works like this:
"" is the root
"a", "b", "c" etc. are the children of ""
"aa", "ab", "ac" etc. are the children of "a"
"ba", "bb", "bc" etc. are the children of "b"
"aaa", "aab", "aac" etc. are the children of "aa"

So there is one character for each level in the tree and the child
nodes of each node are those with the same beginning and one character
added.

How would I do this using an SQLAlchemy mapper without adding an extra
attribute to reference the parent?

Note: I'm only interested in reading the relation ship, if something
like node.children.append(child) isn't working thats fine. Also I'm
stuck to version 0.4.8, however if this is not possible in this
version but only in a newer one I might take the effort to update.

Also on stackoverflow, if you prefer:
http://stackoverflow.com/questions/5471687/self-referencing-table-without-foreign-key-sqlalchemy

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Michael Bayer

On Mar 30, 2011, at 1:39 PM, Hans-Martin wrote:

> On Mar 30, 7:13 pm, Michael Bayer  wrote:
>> well its not going to work there either most likely since we don't support 
>> connecting without a hostname, probably.  You can't put "localhost" in there 
>> ?
> 
> Doesn't work either, same error. Adding my system username (and
> password) then throws errors from the Postgres backend.
> 
> I also installed pg8000 for the 0.6.6 + Py 2.7 setup and this gives
> the same error. With psycopg2, it works. Is there a reason that it
> would stop doing so in 3.2?

OK so its some artifact of the pg8000 dialect.  its nothing Python 3 related.



> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Hans-Martin
On Mar 30, 7:13 pm, Michael Bayer  wrote:
> well its not going to work there either most likely since we don't support 
> connecting without a hostname, probably.  You can't put "localhost" in there ?

Doesn't work either, same error. Adding my system username (and
password) then throws errors from the Postgres backend.

I also installed pg8000 for the 0.6.6 + Py 2.7 setup and this gives
the same error. With psycopg2, it works. Is there a reason that it
would stop doing so in 3.2?

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Michael Bayer
well its not going to work there either most likely since we don't support 
connecting without a hostname, probably.  You can't put "localhost" in there ?


On Mar 30, 2011, at 1:07 PM, Hans-Martin wrote:

>> you'd need an @ sign in there perhaps:
>> 
>> db://@/test
> 
> Same error. But no big deal, I can wait for psycopg2 support in Python
> 3.2.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Hans-Martin
> you'd need an @ sign in there perhaps:
>
> db://@/test

Same error. But no big deal, I can wait for psycopg2 support in Python
3.2.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Michael Bayer

On Mar 30, 2011, at 12:42 PM, Hans-Martin wrote:

> On Mar 30, 5:43 pm, Michael Bayer  wrote:
>> That's not an error I'm familiar with how to reproduce; it suggests an 
>> incorrect string passed to create_engine().   Working code with Python 3.2 
>> plus pg8000 looks like:
>> 
>> from sqlalchemy import create_engine
>> 
>> e = create_engine("postgresql+pg8000://scott:tiger@localhost/test")
>> print(e.execute("select 1").scalar())
> 
> That might be it -- I have the Postgres installation set up to work
> with 'ident sameuser' authentication, so after
> 
> e = create_engine("postgresql+pg8000:///test")
> 
> I get the same error as before.

you'd need an @ sign in there perhaps:

db://@/test


> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Hans-Martin
On Mar 30, 5:43 pm, Michael Bayer  wrote:
> That's not an error I'm familiar with how to reproduce; it suggests an 
> incorrect string passed to create_engine().   Working code with Python 3.2 
> plus pg8000 looks like:
>
> from sqlalchemy import create_engine
>
> e = create_engine("postgresql+pg8000://scott:tiger@localhost/test")
> print(e.execute("select 1").scalar())

That might be it -- I have the Postgres installation set up to work
with 'ident sameuser' authentication, so after

e = create_engine("postgresql+pg8000:///test")

I get the same error as before.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Detect runaway query

2011-03-30 Thread Rick Morrison
Beautiful - there's plenty to work with here. Thanks much, Mike!

On Wed, Mar 30, 2011 at 11:51 AM, Michael Bayer wrote:

> For quick hits I usually turn debug logging on, or echo='debug' with
> create_engine(), that shows the rows coming in in the log.
>
> There's no "per row" event interface, you can of course intercept execute
> events at the engine and cursor level with ConnectionProxy.
>
> A blunt approach may be to intercept cursor executes, ensure the statement
> is a SELECT, then run the statement distinctly (i.e. a second time) in the
> handler, count the rows, or at least do a fetchmany() of N number of rows
> and alert if over a certain threshold.Or wrap the query inside of
> "SELECT COUNT(*) FROM ()" and get a count that way.
>
> Another depends on your DBAPI - if its observed that rows are pre-fetched
> within the execute, then you could just apply timing to a ConnectionProxy
> and look for slow queries (or large jumps in the size of gc.get_objects()
> maybe).
>
> Still another tack, subclass Query, override __iter__, pull out the result
> from super().__iter__(), count it, then return iter(result).  Query by
> default buffers everything anyway.   This would depend though on the fact
> that your query is returning distinct primary keys - if you have a basic
> cartesian product occurring (which is likely), Query's uniquifying of
> results might conceal that.
>
>
> On Mar 30, 2011, at 11:31 AM, Rick Morrison wrote:
>
> > Hi list:
> >
> > I've recently been plagued by a runaway query somewhere in one of my apps
> that mistakenly loads 10s of 1000's of rows, swamping the working set of the
> Python process and eventually invoking the OOM killer.
> >
> > Unfortunately, the database backend I'm using (MSSQL 2005) doesn't
> provide a lot in the way of throttling or detection tools for this, and so
> I'd like to inject some detection code into SQLA to track this thing down.
> Is there an existing listener interface (or an appropriate injection
> location for some code) in the (0.6.6) Engine or ResultProxy where it's
> possible to watch the number of rows retrieved?
> >
> > Thanks,
> > Rick
> >
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> > To post to this group, send email to sqlalchemy@googlegroups.com.
> > To unsubscribe from this group, send email to
> sqlalchemy+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Detect runaway query

2011-03-30 Thread Michael Bayer
For quick hits I usually turn debug logging on, or echo='debug' with 
create_engine(), that shows the rows coming in in the log.

There's no "per row" event interface, you can of course intercept execute 
events at the engine and cursor level with ConnectionProxy.

A blunt approach may be to intercept cursor executes, ensure the statement is a 
SELECT, then run the statement distinctly (i.e. a second time) in the handler, 
count the rows, or at least do a fetchmany() of N number of rows and alert if 
over a certain threshold.Or wrap the query inside of "SELECT COUNT(*) FROM 
()" and get a count that way.

Another depends on your DBAPI - if its observed that rows are pre-fetched 
within the execute, then you could just apply timing to a ConnectionProxy and 
look for slow queries (or large jumps in the size of gc.get_objects() maybe).

Still another tack, subclass Query, override __iter__, pull out the result from 
super().__iter__(), count it, then return iter(result).  Query by default 
buffers everything anyway.   This would depend though on the fact that your 
query is returning distinct primary keys - if you have a basic cartesian 
product occurring (which is likely), Query's uniquifying of results might 
conceal that.


On Mar 30, 2011, at 11:31 AM, Rick Morrison wrote:

> Hi list:
> 
> I've recently been plagued by a runaway query somewhere in one of my apps 
> that mistakenly loads 10s of 1000's of rows, swamping the working set of the 
> Python process and eventually invoking the OOM killer. 
> 
> Unfortunately, the database backend I'm using (MSSQL 2005) doesn't provide a 
> lot in the way of throttling or detection tools for this, and so I'd like to 
> inject some detection code into SQLA to track this thing down. Is there an 
> existing listener interface (or an appropriate injection location for some 
> code) in the (0.6.6) Engine or ResultProxy where it's possible to watch the 
> number of rows retrieved? 
> 
> Thanks,
> Rick
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Michael Bayer
That's not an error I'm familiar with how to reproduce; it suggests an 
incorrect string passed to create_engine().   Working code with Python 3.2 plus 
pg8000 looks like:

from sqlalchemy import create_engine

e = create_engine("postgresql+pg8000://scott:tiger@localhost/test")
print(e.execute("select 1").scalar())


Note that we're at 99.7% test success for Python 3.2 + pg8000, there's only 8 
failures which are basically platform-dependent issues with the tests 
themselves:

http://paste.pocoo.org/show/362703/





On Mar 30, 2011, at 11:13 AM, Hans-Martin wrote:

> Sorry, I was not expecting it to work, just trying to follow your
> suggestion.
> 
> SQLAlchemy Dev snapshot + pg8000 from 
> http://pybrary.net/pg8000/dist/pg8000-py3-1.08.tar.gz
> throws this error:
> 
> Traceback (most recent call last):
>  File "set_meta_info_levels_0_1.py", line 60, in 
>set_up_schemata()
>  File " src/library/db_connect.py", line 64, in set_up_schemata
>if s not in existing_schemata():
>  File " src/library/db_connect.py", line 60, in existing_schemata
>tmp = session.execute("""SELECT schema_name FROM
> information_schema.schemata;""")
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/orm/session.py", line 758, in execute
>return self._connection_for_bind(bind,
> close_with_result=True).execute(
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/orm/session.py", line 694, in _connection_for_bind
>return self.transaction._connection_for_bind(engine)
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/orm/session.py", line 246, in _connection_for_bind
>conn = bind.contextual_connect()
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/base.py", line 2062, in contextual_connect
>self.pool.connect(),
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 208, in connect
>return _ConnectionFairy(self).checkout()
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 369, in __init__
>rec = self._connection_record = pool._do_get()
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 695, in _do_get
>con = self._create_connection()
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 173, in _create_connection
>return _ConnectionRecord(self)
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 254, in __init__
>self.connection = self.__connect()
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 314, in __connect
>connection = self.__pool._creator()
>  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/strategies.py", line 84, in connect
>e, dialect.dbapi.Error) from e
> sqlalchemy.exc.DBAPIError: (TypeError) connect() takes at least 1
> argument (1 given) None None
> 
> 
> I have a working setup with 2.7 + 0.6.6, but would like to port it to
> 3.2 relatively soon -- if I can help trying out things, just let me
> know.
> 
> Best wishes,
> Hans-Martin
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Detect runaway query

2011-03-30 Thread Rick Morrison
Hi list:

I've recently been plagued by a runaway query somewhere in one of my apps
that mistakenly loads 10s of 1000's of rows, swamping the working set of the
Python process and eventually invoking the OOM killer.

Unfortunately, the database backend I'm using (MSSQL 2005) doesn't provide a
lot in the way of throttling or detection tools for this, and so I'd like to
inject some detection code into SQLA to track this thing down. Is there an
existing listener interface (or an appropriate injection location for some
code) in the (0.6.6) Engine or ResultProxy where it's possible to watch the
number of rows retrieved?

Thanks,
Rick

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



RE: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could not find table with which to generate a foreign key

2011-03-30 Thread King Simon-NFHD78
Something else must be importing those modules when run from pylons. If you 
really want to know how they are getting imported, stick something in the 
module which will raise an exception when it is imported (eg type "blah blah 
blah" at the top of the module) and look at the traceback.

Cheers,

Simon

-Original Message-
From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On 
Behalf Of 371c
Sent: 30 March 2011 16:00
To: sqlalchemy
Subject: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could not find 
table with which to generate a foreign key

Actually that helps alot and i had infact resolved to  and was in the
process of doing so  (i know that about python modules.. ;) but i
might be missing something)

But, why does this work in the context of an application (eg. pylons
app). Basically, calling Base.create_all() in some init_db method of
an application
works without having to import all the modules in, say,
myapp.models.__init__.py

Suggestions are welcome, though i'm considering the question answered

Thanks alot and Regards,


On Mar 30, 4:39 pm, "King Simon-NFHD78"
 wrote:
> This is just the way Python works - code inside a module is only
> executed when that module is imported. If you don't import
> myapp.models.notes, then the class definitions never get executed.
>
> One solution is to import all the sub-modules in your bootstrap.py
> before calling create_all. Another is importing the submodules inside
> the myapp/models/__init__.py
>
> Hope that helps,
>
> Simon
>
> -Original Message-
> From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
>
> On Behalf Of eric cire
> Sent: 30 March 2011 14:57
> To: sqlalchemy
> Subject: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could
> not find table with which to generate a foreign key
>
> After investigating further, i have the impression that the problem
> occurs when models are in different modules (notes.py & users.py in this
> case) but if the models are in the same module eg.
> myapp.models.__init__.py, the tables are created.
>
> I'd still like to know why this is happening because i don't intend to
> put al my models in the same module..
>
> Thanks,
>
> On Wed, Mar 30, 2011 at 3:36 PM, 371c <371c@gmail.com> wrote:
>
>         Hi,
>
>         I have the following setup:
>
>         myapp.models.notes.py
>         Note model defined here using declarative base
>
>         myapp.models.users.py
>         User model defined here using declarative base
>
>         myapp.models.meta.py
>         Base and DBSession defined here to avoid circular imports...
>
>         myapp.lib.bootstrap.py
>         Called to initialize the database with some initial data. The
>         following is done:
>         create an engine (sqlite:///notes.db)
>         call Base.create_all(bind=engine)
>
>         The Base class is the same for the models and the bootstrap.py
> module,
>         but i still get a noreferencedtableerror...
>
>         it basically doesn't create the database tables when
> bootstrap.py is
>         called..
>
>         Any ideas ?
>
>         Regards,
>
> --
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/sqlalchemy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could not find table with which to generate a foreign key

2011-03-30 Thread 371c
Actually that helps alot and i had infact resolved to  and was in the
process of doing so  (i know that about python modules.. ;) but i
might be missing something)

But, why does this work in the context of an application (eg. pylons
app). Basically, calling Base.create_all() in some init_db method of
an application
works without having to import all the modules in, say,
myapp.models.__init__.py

Suggestions are welcome, though i'm considering the question answered

Thanks alot and Regards,


On Mar 30, 4:39 pm, "King Simon-NFHD78"
 wrote:
> This is just the way Python works - code inside a module is only
> executed when that module is imported. If you don't import
> myapp.models.notes, then the class definitions never get executed.
>
> One solution is to import all the sub-modules in your bootstrap.py
> before calling create_all. Another is importing the submodules inside
> the myapp/models/__init__.py
>
> Hope that helps,
>
> Simon
>
> -Original Message-
> From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
>
> On Behalf Of eric cire
> Sent: 30 March 2011 14:57
> To: sqlalchemy
> Subject: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could
> not find table with which to generate a foreign key
>
> After investigating further, i have the impression that the problem
> occurs when models are in different modules (notes.py & users.py in this
> case) but if the models are in the same module eg.
> myapp.models.__init__.py, the tables are created.
>
> I'd still like to know why this is happening because i don't intend to
> put al my models in the same module..
>
> Thanks,
>
> On Wed, Mar 30, 2011 at 3:36 PM, 371c <371c@gmail.com> wrote:
>
>         Hi,
>
>         I have the following setup:
>
>         myapp.models.notes.py
>         Note model defined here using declarative base
>
>         myapp.models.users.py
>         User model defined here using declarative base
>
>         myapp.models.meta.py
>         Base and DBSession defined here to avoid circular imports...
>
>         myapp.lib.bootstrap.py
>         Called to initialize the database with some initial data. The
>         following is done:
>         create an engine (sqlite:///notes.db)
>         call Base.create_all(bind=engine)
>
>         The Base class is the same for the models and the bootstrap.py
> module,
>         but i still get a noreferencedtableerror...
>
>         it basically doesn't create the database tables when
> bootstrap.py is
>         called..
>
>         Any ideas ?
>
>         Regards,
>
> --
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/sqlalchemy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Hans-Martin
Sorry, I was not expecting it to work, just trying to follow your
suggestion.

SQLAlchemy Dev snapshot + pg8000 from 
http://pybrary.net/pg8000/dist/pg8000-py3-1.08.tar.gz
throws this error:

Traceback (most recent call last):
  File "set_meta_info_levels_0_1.py", line 60, in 
set_up_schemata()
  File " src/library/db_connect.py", line 64, in set_up_schemata
if s not in existing_schemata():
  File " src/library/db_connect.py", line 60, in existing_schemata
tmp = session.execute("""SELECT schema_name FROM
information_schema.schemata;""")
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/orm/session.py", line 758, in execute
return self._connection_for_bind(bind,
close_with_result=True).execute(
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/orm/session.py", line 694, in _connection_for_bind
return self.transaction._connection_for_bind(engine)
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/orm/session.py", line 246, in _connection_for_bind
conn = bind.contextual_connect()
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/base.py", line 2062, in contextual_connect
self.pool.connect(),
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 208, in connect
return _ConnectionFairy(self).checkout()
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 369, in __init__
rec = self._connection_record = pool._do_get()
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 695, in _do_get
con = self._create_connection()
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 173, in _create_connection
return _ConnectionRecord(self)
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 254, in __init__
self.connection = self.__connect()
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 314, in __connect
connection = self.__pool._creator()
  File " lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/strategies.py", line 84, in connect
e, dialect.dbapi.Error) from e
sqlalchemy.exc.DBAPIError: (TypeError) connect() takes at least 1
argument (1 given) None None


I have a working setup with 2.7 + 0.6.6, but would like to port it to
3.2 relatively soon -- if I can help trying out things, just let me
know.

Best wishes,
Hans-Martin

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could not find table with which to generate a foreign key

2011-03-30 Thread 371c
Actually that helps alot and i had infact resolved to  and was in the
process of doing so  (i know that about python modules.. ;) but i
might be missing something)

But, why does this work in the context of an application (eg. pylons
app). Basically, calling Base.create_all() in some init_db method of
an application
works without having to import all the modules in, say,
myapp.models.__init__.py

Suggestions are welcome, though i'm considering the question answered

Thanks alot and Regards,


On Mar 30, 4:39 pm, "King Simon-NFHD78"
 wrote:
> This is just the way Python works - code inside a module is only
> executed when that module is imported. If you don't import
> myapp.models.notes, then the class definitions never get executed.
>
> One solution is to import all the sub-modules in your bootstrap.py
> before calling create_all. Another is importing the submodules inside
> the myapp/models/__init__.py
>
> Hope that helps,
>
> Simon
>
> -Original Message-
> From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
>
> On Behalf Of eric cire
> Sent: 30 March 2011 14:57
> To: sqlalchemy
> Subject: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could
> not find table with which to generate a foreign key
>
> After investigating further, i have the impression that the problem
> occurs when models are in different modules (notes.py & users.py in this
> case) but if the models are in the same module eg.
> myapp.models.__init__.py, the tables are created.
>
> I'd still like to know why this is happening because i don't intend to
> put al my models in the same module..
>
> Thanks,
>
> On Wed, Mar 30, 2011 at 3:36 PM, 371c <371c@gmail.com> wrote:
>
>         Hi,
>
>         I have the following setup:
>
>         myapp.models.notes.py
>         Note model defined here using declarative base
>
>         myapp.models.users.py
>         User model defined here using declarative base
>
>         myapp.models.meta.py
>         Base and DBSession defined here to avoid circular imports...
>
>         myapp.lib.bootstrap.py
>         Called to initialize the database with some initial data. The
>         following is done:
>         create an engine (sqlite:///notes.db)
>         call Base.create_all(bind=engine)
>
>         The Base class is the same for the models and the bootstrap.py
> module,
>         but i still get a noreferencedtableerror...
>
>         it basically doesn't create the database tables when
> bootstrap.py is
>         called..
>
>         Any ideas ?
>
>         Regards,
>
> --
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/sqlalchemy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



RE: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could not find table with which to generate a foreign key

2011-03-30 Thread King Simon-NFHD78
This is just the way Python works - code inside a module is only
executed when that module is imported. If you don't import
myapp.models.notes, then the class definitions never get executed.

One solution is to import all the sub-modules in your bootstrap.py
before calling create_all. Another is importing the submodules inside
the myapp/models/__init__.py

Hope that helps,

Simon 

-Original Message-
From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com]
On Behalf Of eric cire
Sent: 30 March 2011 14:57
To: sqlalchemy
Subject: [sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could
not find table with which to generate a foreign key

After investigating further, i have the impression that the problem
occurs when models are in different modules (notes.py & users.py in this
case) but if the models are in the same module eg.
myapp.models.__init__.py, the tables are created.

I'd still like to know why this is happening because i don't intend to
put al my models in the same module..

Thanks,


On Wed, Mar 30, 2011 at 3:36 PM, 371c <371c@gmail.com> wrote:


Hi,

I have the following setup:

myapp.models.notes.py
Note model defined here using declarative base

myapp.models.users.py
User model defined here using declarative base

myapp.models.meta.py
Base and DBSession defined here to avoid circular imports...

myapp.lib.bootstrap.py
Called to initialize the database with some initial data. The
following is done:
create an engine (sqlite:///notes.db)
call Base.create_all(bind=engine)

The Base class is the same for the models and the bootstrap.py
module,
but i still get a noreferencedtableerror...

it basically doesn't create the database tables when
bootstrap.py is
called..

Any ideas ?

Regards,








-- 
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Michael Bayer

On Mar 30, 2011, at 8:31 AM, Hans-Martin wrote:

>> On Mar 21, 3:31 pm, Michael Bayer  wrote:
>> 
>>> Yeah psycopg2 with python 3 / sqlalchemy is not supported yet.  The python 
>>> 3 supported version of psycopg2 came out like, in the past two weeks.It 
>>> will be 0.7 where its supported at all, hopefully soon as its a matter of 
>>> setting up some flags in the dialects. In the meantime if you want to 
>>> start working with pg + SQLA + py3k you can try the python 3 version of 
>>> pg8000 - its not as nice or anywhere near as fast as psycopg2 but it works 
>>> in at least a rudimentary fashion.
>> 
>>> Also Python 3.2 is the latest version of py3k and actually has some fairly 
>>> dramatic behavioral differences vs. 3.1.   If you want to work with 3.2 and 
>>> let us know what quirks you find...
> 
> Below is one (Python 3.2, sqlalchemy 0.7b4 (downloaded snapshot 10min
> ago), Debian). Best wishes, Hans-Martin

As I said, psycopg2 does not work *at all* yet, with SQLAlchemy plus any Python 
3 version - the dialect has not been ported, so the issue below is just a 
rudimentary Python 3 issue.   You'd need to use pg8000 for the time being, 
which was ported at least a year ago.




> 
> Traceback (most recent call last):
>  File "src/library/db_connect.py", line 60, in existing_schemata
>tmp = session.execute("""SELECT schema_name from
> information_schema.schemata;""")
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/orm/session.py", line 758, in execute
>return self._connection_for_bind(bind,
> close_with_result=True).execute(
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/orm/session.py", line 694, in _connection_for_bind
>return self.transaction._connection_for_bind(engine)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/orm/session.py", line 246, in _connection_for_bind
>conn = bind.contextual_connect()
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/base.py", line 2062, in contextual_connect
>self.pool.connect(),
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 208, in connect
>return _ConnectionFairy(self).checkout()
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 369, in __init__
>rec = self._connection_record = pool._do_get()
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 695, in _do_get
>con = self._create_connection()
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 173, in _create_connection
>return _ConnectionRecord(self)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/pool.py", line 257, in __init__
>pool.dispatch.first_connect.exec_once(self.connection, self)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/event.py", line 227, in exec_once
>self(*args, **kw)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/event.py", line 236, in __call__
>fn(*args, **kw)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/strategies.py", line 162, in first_connect
>dialect.initialize(c)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/dialects/postgresql/base.py", line 793, in initialize
>super(PGDialect, self).initialize(connection)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/default.py", line 171, in initialize
>self._get_server_version_info(connection)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/dialects/postgresql/base.py", line 964, in
> _get_server_version_info
>v = connection.execute("select version()").scalar()
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/base.py", line 1259, in execute
>params)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/base.py", line 1436, in _execute_text
>statement, parameters
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/base.py", line 1519, in _execute_context
>result = context.get_result_proxy()
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/dialects/postgresql/psycopg2.py", line 198, in
> get_result_proxy
>return base.ResultProxy(self)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/base.py", line 2420, in __init__
>self._init_metadata()
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/base.py", line 2427, in _init_metadata
>self._metadata = ResultMetaData(self, metadata)
>  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
> sqlalchemy/engine/base.py", line 2

[sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could not find table with which to generate a foreign key

2011-03-30 Thread eric cire
Hi, i can actually confirm that i don't get the error when all models are in
the same module. Any ideas?

On Wed, Mar 30, 2011 at 3:57 PM, eric cire <371c@gmail.com> wrote:

> After investigating further, i have the impression that the problem occurs
> when models are in different modules (notes.py & users.py in this case) but
> if the models are in the same module eg. myapp.models.__init__.py, the
> tables are created.
>
> I'd still like to know why this is happening because i don't intend to put
> al my models in the same module..
>
> Thanks,
>
>
> On Wed, Mar 30, 2011 at 3:36 PM, 371c <371c@gmail.com> wrote:
>
>> Hi,
>>
>> I have the following setup:
>>
>> myapp.models.notes.py
>> Note model defined here using declarative base
>>
>> myapp.models.users.py
>> User model defined here using declarative base
>>
>> myapp.models.meta.py
>> Base and DBSession defined here to avoid circular imports...
>>
>> myapp.lib.bootstrap.py
>> Called to initialize the database with some initial data. The
>> following is done:
>> create an engine (sqlite:///notes.db)
>> call Base.create_all(bind=engine)
>>
>> The Base class is the same for the models and the bootstrap.py module,
>> but i still get a noreferencedtableerror...
>>
>> it basically doesn't create the database tables when  bootstrap.py is
>> called..
>>
>> Any ideas ?
>>
>> Regards,
>>
>>
>>
>>
>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: sqlalchemy.exc.NoReferencedTableError: Could not find table with which to generate a foreign key

2011-03-30 Thread eric cire
After investigating further, i have the impression that the problem occurs
when models are in different modules (notes.py & users.py in this case) but
if the models are in the same module eg. myapp.models.__init__.py, the
tables are created.

I'd still like to know why this is happening because i don't intend to put
al my models in the same module..

Thanks,

On Wed, Mar 30, 2011 at 3:36 PM, 371c <371c@gmail.com> wrote:

> Hi,
>
> I have the following setup:
>
> myapp.models.notes.py
> Note model defined here using declarative base
>
> myapp.models.users.py
> User model defined here using declarative base
>
> myapp.models.meta.py
> Base and DBSession defined here to avoid circular imports...
>
> myapp.lib.bootstrap.py
> Called to initialize the database with some initial data. The
> following is done:
> create an engine (sqlite:///notes.db)
> call Base.create_all(bind=engine)
>
> The Base class is the same for the models and the bootstrap.py module,
> but i still get a noreferencedtableerror...
>
> it basically doesn't create the database tables when  bootstrap.py is
> called..
>
> Any ideas ?
>
> Regards,
>
>
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] sqlalchemy.exc.NoReferencedTableError: Could not find table with which to generate a foreign key

2011-03-30 Thread 371c
Hi,

I have the following setup:

myapp.models.notes.py
Note model defined here using declarative base

myapp.models.users.py
User model defined here using declarative base

myapp.models.meta.py
Base and DBSession defined here to avoid circular imports...

myapp.lib.bootstrap.py
Called to initialize the database with some initial data. The
following is done:
create an engine (sqlite:///notes.db)
call Base.create_all(bind=engine)

The Base class is the same for the models and the bootstrap.py module,
but i still get a noreferencedtableerror...

it basically doesn't create the database tables when  bootstrap.py is
called..

Any ideas ?

Regards,





-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Re: Python 3.1 + SQLAlchemy 0.6 or 0.7

2011-03-30 Thread Hans-Martin
> On Mar 21, 3:31 pm, Michael Bayer  wrote:
>
> > Yeah psycopg2 with python 3 / sqlalchemy is not supported yet.  The python 
> > 3 supported version of psycopg2 came out like, in the past two weeks.    It 
> > will be 0.7 where its supported at all, hopefully soon as its a matter of 
> > setting up some flags in the dialects.     In the meantime if you want to 
> > start working with pg + SQLA + py3k you can try the python 3 version of 
> > pg8000 - its not as nice or anywhere near as fast as psycopg2 but it works 
> > in at least a rudimentary fashion.
>
> > Also Python 3.2 is the latest version of py3k and actually has some fairly 
> > dramatic behavioral differences vs. 3.1.   If you want to work with 3.2 and 
> > let us know what quirks you find...

Below is one (Python 3.2, sqlalchemy 0.7b4 (downloaded snapshot 10min
ago), Debian). Best wishes, Hans-Martin

Traceback (most recent call last):
  File "src/library/db_connect.py", line 60, in existing_schemata
tmp = session.execute("""SELECT schema_name from
information_schema.schemata;""")
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/orm/session.py", line 758, in execute
return self._connection_for_bind(bind,
close_with_result=True).execute(
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/orm/session.py", line 694, in _connection_for_bind
return self.transaction._connection_for_bind(engine)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/orm/session.py", line 246, in _connection_for_bind
conn = bind.contextual_connect()
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/base.py", line 2062, in contextual_connect
self.pool.connect(),
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 208, in connect
return _ConnectionFairy(self).checkout()
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 369, in __init__
rec = self._connection_record = pool._do_get()
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 695, in _do_get
con = self._create_connection()
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 173, in _create_connection
return _ConnectionRecord(self)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/pool.py", line 257, in __init__
pool.dispatch.first_connect.exec_once(self.connection, self)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/event.py", line 227, in exec_once
self(*args, **kw)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/event.py", line 236, in __call__
fn(*args, **kw)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/strategies.py", line 162, in first_connect
dialect.initialize(c)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/dialects/postgresql/base.py", line 793, in initialize
super(PGDialect, self).initialize(connection)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/default.py", line 171, in initialize
self._get_server_version_info(connection)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/dialects/postgresql/base.py", line 964, in
_get_server_version_info
v = connection.execute("select version()").scalar()
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/base.py", line 1259, in execute
params)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/base.py", line 1436, in _execute_text
statement, parameters
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/base.py", line 1519, in _execute_context
result = context.get_result_proxy()
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/dialects/postgresql/psycopg2.py", line 198, in
get_result_proxy
return base.ResultProxy(self)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/base.py", line 2420, in __init__
self._init_metadata()
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/base.py", line 2427, in _init_metadata
self._metadata = ResultMetaData(self, metadata)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/engine/base.py", line 2283, in __init__
colname = dialect._description_decoder(colname)
  File "lib/python3.2/site-packages/SQLAlchemy-0.7b4dev-py3.2.egg/
sqlalchemy/processors.py", line 69, in process
return decoder(value, errors)[0]
  File "lib/python3.2/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
TypeError: 'str' does not support the buffer interface

-- 
You received this message because you are s

Re: [sqlalchemy] MultipleResultsFound

2011-03-30 Thread jose soares

Mike Conley wrote:
You issued a query with a .one() qualifier and there is more than one 
row in the database satisfying the condition.


Example: 2 names in a table
firstname="pete", lastname="smith"
firstname="john", lastname="smith"

query for rows lastname="smith" with .one() will fail because there 
are 2 "smith" in database


--
Mike Conley



but I'm not explicit using this .one() in my code...



Traceback (most recent call last):
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/cherrypy/_cphttptools.py",
 line 121, in _run
   self.main()
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/cherrypy/_cphttptools.py",
 line 264, in main
   body = page_handler(*virtual_path, **self.params)
 File "", line 3, in index
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/TurboGears-1.1.1-py2.6.egg/turbogears/identity/conditions.py",
 line 246, in require
   predicate.eval_with_object(current, errors):
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/TurboGears-1.1.1-py2.6.egg/turbogears/identity/conditions.py",
 line 88, in eval_with_object
   if p.eval_with_object(obj, None):
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/TurboGears-1.1.1-py2.6.egg/turbogears/identity/conditions.py",
 line 171, in eval_with_object
   if self.permission_name in identity.permissions:
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/TurboGears-1.1.1-py2.6.egg/turbogears/identity/base.py",
 line 173, in __getattr__
   return getattr(identity, name)
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/TurboGears-1.1.1-py2.6.egg/turbogears/identity/saprovider.py",
 line 76, in permissions
   p.permission_name for p in self.user.permissions)
 File "/home/users/admin/release/sicer/BASE/controller/errorhandling.py", line 
33, in _get_user
   return saprovider.user_class.query.get(self.visit_link.user_id)
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/SQLAlchemy-0.6.0-py2.6.egg/sqlalchemy/orm/query.py",
 line 549, in get
   return self._get(key, ident)
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/SQLAlchemy-0.6.0-py2.6.egg/sqlalchemy/orm/query.py",
 line 1675, in _get
   return q.one()
 File 
"/home/users/admin/.VE-SICER2.x-x86_64-19.04.2010/lib/python2.6/site-packages/SQLAlchemy-0.6.0-py2.6.egg/sqlalchemy/orm/query.py",
 line 1417, in one
   "Multiple rows were found for one()")
MultipleResultsFound: Multiple rows were found for one()

j

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] Truncate tables on unittest.TestCase.tearDown Hangs

2011-03-30 Thread Corey Coogan
I have a couple tables I would like to truncate when each integration
test is done running.  This works fine, in some cases, as long as
there's exception thrown from any of the tests.  In other cases, only
the first test works and the others hang on the TRUNCATE call.

I know it must have something to do with connections to the database
or something like that, but I'm not sure how.  I am closing the
Session after each test and even tried to disable the connection
pooling.  My teardown calls rollback() on the session just in case
there was an Exception or failed assertion.

The code I'm using to truncate is pretty basic and uses a newly
configured engine that has pooling disabled.:

engine = create_engine(
'postgresql://%s:%s@%s/%s' %
(Settings.DB_USER, Settings.DB_PASSWD, Settings.DB_HOST,
dataBaseName),
echo=True,
poolclass=NullPool
)
with contextlib.closing(engine.connect()) as con:
trans = con.begin()
con.execute('truncate table "Schema"."Table" cascade')
trans.commit()
engine = None



Is there something else I need to do to ensure all DB connections are
closed?  I'm using PG 9.0, SA 0.6.6 and PY 2.66.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Re: Combine Textual query with a filter

2011-03-30 Thread Corey Coogan
Thanks again Michael.  The syntax was just made up pseudo code.  I was in a 
hurry to respond.

Anyway, this worked and here's a snippet of what it looks like:

sharedFilterQuery = '''WITHRECURSIVE
q AS
(
SELECT  h.*
FROM"Selection"."FilterFolder" h
join "Selection"."Filter" f
on f."filterFolderId" = h.id
WHERE   f.id = :filterId 
UNION
SELECT  hp.*
FROMq
JOIN"Selection"."FilterFolder" hp
ON  hp.id = q."parentFolderId"
)
SELECT  f.id
FROM"Selection"."Filter" f
where f.id = :filterId and
(f."createdByUserId" = 1 or
exists(select 1 from q where "isShared" = TRUE LIMIT 1))
'''
inClause = 
text(sharedFilterQuery,bindparams=[bindparam('filterId',filterId)])
f = session.query(Filter).filter(Filter.description == None)\
.filter(Filter.id.in_(inClause)).first()

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] MultipleResultsFound

2011-03-30 Thread Mike Conley
You issued a query with a .one() qualifier and there is more than one row in
the database satisfying the condition.

Example: 2 names in a table
firstname="pete", lastname="smith"
firstname="john", lastname="smith"

query for rows lastname="smith" with .one() will fail because there are 2
"smith" in database

-- 
Mike Conley

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



[sqlalchemy] MultipleResultsFound

2011-03-30 Thread jose soares

Hi all,

I got, for the first time the following error:

"../lib/python2.6/site-packages/SQLAlchemy-0.6.0-py2.6.egg/sqlalchemy/orm/query.py",
 line 1417, in one
   "Multiple rows were found for one()")
MultipleResultsFound: Multiple rows were found for one()


Does anyone know what that means?

j


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.