Re: [sqlalchemy] Re: How can I use and_ and or_ in the orm query ?

2012-05-11 Thread Michael Bayer
we have this exact usage described right in the ORM tutorial:

http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html#common-filter-operators

On May 11, 2012, at 5:55 AM, Jonathan Vanasco wrote:

 Dan Kuebrich messaged me off-list -- the filter() method seems to
 accept the output of and_  or_
 
 it would be great if the docs in each section referenced this.
 
 On May 10, 5:19 pm, Jonathan Vanasco jonat...@findmeon.com wrote:
 they're in the sql.expression , and the orm.query object doesn't have
 those methods.  what query method do i use to integrate them ?
 
 -- 
 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] How should I do inheritance using DeclarativeReflectedBase?

2012-05-11 Thread Ignas Mikalajunas
Even though the latest version of the DeclarativeRefletive example
includes some handling for inheritance, I still can not get it to
work. I try doing (mostly modified example from
https://bitbucket.org/sqlalchemy/sqlalchemy/src/408388e5faf4/examples/declarative_reflection/declarative_reflection.py):

from sqlalchemy.types import Integer
from sqlalchemy.types import String
from sqlalchemy.schema import ForeignKey
from sqlalchemy.schema import Column
from sqlalchemy.schema import Table
from sqlalchemy.orm import mapper
from sqlalchemy.orm.session import Session
from sqlalchemy.orm.util import _is_mapped_class
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base

class DeclarativeReflectedBase(object):
_mapper_args = []

@classmethod
def __mapper_cls__(cls, *args, **kw):
Declarative will use this function in lieu of
calling mapper() directly.

Collect each series of arguments and invoke
them when prepare() is called.


cls._mapper_args.append((args, kw))

@classmethod
def prepare(cls, engine):
Reflect all the tables and map !
while cls._mapper_args:
args, kw  = cls._mapper_args.pop()
klass = args[0]
# autoload Table, which is already
# present in the metadata.  This
# will fill in db-loaded columns
# into the existing Table object.
if args[1] is not None:
table = args[1]
Table(table.name,
cls.metadata,
extend_existing=True,
autoload_replace=False,
autoload=True,
autoload_with=engine,
schema=table.schema)

# see if we need 'inherits' in the
# mapper args.  Declarative will have
# skipped this since mappings weren't
# available yet.
for c in klass.__bases__:
if _is_mapped_class(c):
kw['inherits'] = c
break

klass.__mapper__ = mapper(*args, **kw)

if __name__ == '__main__':
Base = declarative_base()

# create a separate base so that we can
# define a subset of classes as Reflected,
# instead of everything.
class Reflected(DeclarativeReflectedBase, Base):
__abstract__ = True

class Foo(Reflected):
__tablename__ = 'foo'

type_ = Column('type', String(32))
__mapper_args__ = {'polymorphic_on': type_,
   'polymorphic_identity': 'foo'}


class Bar(Foo):
__tablename__ = 'bar'
__mapper_args__ = {'polymorphic_identity': 'bar'}

id = Column(Integer, ForeignKey('foo.id'), primary_key=True)

e = create_engine('sqlite://', echo=True)
e.execute(
create table foo(
id integer primary key,
type varchar(32),
data varchar(30)
)
)

e.execute(
create table bar(
id integer primary key,
bar_data varchar(30)
)
)

Reflected.prepare(e)

s = Session(e)

s.add_all([
Bar(data='d1', bar_data='b1'),
Bar(data='d2', bar_data='b2'),
Bar(data='d3', bar_data='b3'),
Foo(data='d4')
])
s.commit()
for f in s.query(Foo):
print f.data, getattr(f, 'bar_data', 'not_a_bar')

and sqlalchemy tries to find the type column in the table 'bar'. Am I
doing the inheritance set up wrong or is it some bug in
DeclarativeReflectiveBase?

Ignas

-- 
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] Mapper compilation errors in multi threaded web application using dynamic mapping to selects

2012-05-11 Thread Jochen Stenzel
(Sorry if this message appears twice in the list - I first sent it 
using an unregistered mail address.)


Hello,

thank you for your quick reply.

is there a problem in mapping classes to selects ([1]) /within a 
function/?


with multiple threads, where the mappers initialization may first
proceed as the product of a thread running, yes. you'd want to
upgrade to 0.7 for the best versions of these fixes, or at least 0.6.

If you must stay on 0.5, make sure all modules are fully imported,
then run compile_mappers() before starting any threads.


I tried this first as migrating to version 0.7 or 0.6 sounds like a 
significant (and currently unplanned) effort, so I hoped we could stay 
with 0.5 for the moment.


So I identified our standard mapper calls to tables. It turned out 
they are invoked by a single function running in the startup phase, so I 
added a call to compile_mappers() at the end of this function. The 
function is invoked very early and even before the __init__() method of 
the WSGI application object - I hope this is early enough. Looking at 
the server logs one can see it is called right after server startup, as 
often as initial processes are configured.


Unfortunately, there are still errors displayed. Most of the time I 
found 'Mapper' object has no attribute '_props'.


I also tried to use the standard SQLAlchemy mapper() function instead 
of our wrapper mentioned in my first message (a variant of [1]). In most 
cases the system complained about the _props attribute as before, in 
rare cases the message dictionary changed size during iteration 
appeared. (I switched back to the standard mapper to find out if our 
wrapper could have an influence.)


Am I correct that the results indicate we *have* to switch to 0.6/0.7 
for a solution, or did I oversee something? From your message:



If you must stay on 0.5, make sure all modules are fully imported,
then run compile_mappers() before starting any threads.


Could it be I am not loading enough modules? Does all modules mean 
all modules of the application, or all modules to map successfully?


Thanks and regards

 Jochen


Reference:

[1] http://www.sqlalchemy.org/trac/wiki/UsageRecipes/SessionAwareMapper


--
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] How should I do inheritance using DeclarativeReflectedBase?

2012-05-11 Thread Michael Bayer

On May 11, 2012, at 1:16 PM, Ignas Mikalajunas wrote:

 Even though the latest version of the DeclarativeRefletive example
 includes some handling for inheritance, I still can not get it to
 work. I try doing (mostly modified example from
 https://bitbucket.org/sqlalchemy/sqlalchemy/src/408388e5faf4/examples/declarative_reflection/declarative_reflection.py):

 
 and sqlalchemy tries to find the type column in the table 'bar'. Am I
 doing the inheritance set up wrong or is it some bug in
 DeclarativeReflectiveBase?

would need to spend some time with it, the declarativereflective example hasn't 
been worked out for setting up an inheritance relationship as of yet.   So 
there could be any number of issues with it (part of why it's only an example 
and not a real feature)..


-- 
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] Mapper compilation errors in multi threaded web application using dynamic mapping to selects

2012-05-11 Thread Michael Bayer

On May 11, 2012, at 1:31 PM, Jochen Stenzel wrote:

 (Sorry if this message appears twice in the list - I first sent it using an 
 unregistered mail address.)
 
 Hello,
 
 thank you for your quick reply.
 
 is there a problem in mapping classes to selects ([1]) /within a function/?
 
 with multiple threads, where the mappers initialization may first
 proceed as the product of a thread running, yes. you'd want to
 upgrade to 0.7 for the best versions of these fixes, or at least 0.6.
 
 If you must stay on 0.5, make sure all modules are fully imported,
 then run compile_mappers() before starting any threads.
 
 I tried this first as migrating to version 0.7 or 0.6 sounds like a 
 significant (and currently unplanned) effort, so I hoped we could stay with 
 0.5 for the moment.
 
 So I identified our standard mapper calls to tables. It turned out they are 
 invoked by a single function running in the startup phase, so I added a call 
 to compile_mappers() at the end of this function. The function is invoked 
 very early and even before the __init__() method of the WSGI application 
 object - I hope this is early enough. Looking at the server logs one can see 
 it is called right after server startup, as often as initial processes are 
 configured.
 
 Unfortunately, there are still errors displayed. Most of the time I found 
 'Mapper' object has no attribute '_props'.
 
 I also tried to use the standard SQLAlchemy mapper() function instead of our 
 wrapper mentioned in my first message (a variant of [1]). In most cases the 
 system complained about the _props attribute as before, in rare cases the 
 message dictionary changed size during iteration appeared. (I switched back 
 to the standard mapper to find out if our wrapper could have an influence.)
 
 Am I correct that the results indicate we *have* to switch to 0.6/0.7 for a 
 solution, or did I oversee something? From your message:
 
 If you must stay on 0.5, make sure all modules are fully imported,
 then run compile_mappers() before starting any threads.
 
 Could it be I am not loading enough modules? Does all modules mean all 
 modules of the application, or all modules to map successfully?

yes the issue is very likely that more modules are being imported within 
non-main threads, and more mappers are coming in.   if you get absolutely every 
mapper loaded up, then call compile_mappers() before any threads spawn, this 
kind of problem shouldn't occur.  

There may be other bugs in mapper configuration in 0.5 though this seems quite 
unusual that you're getting threading errors this frequently (especially 
dictionary changed size  ?  stack trace on that ?) .   Trying 0.6 at least 
might be worth it.


-- 
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] Mapper compilation errors in multi threaded web application using dynamic mapping to selects

2012-05-11 Thread Jochen Stenzel


Could it be I am not loading enough modules? Does all modules mean 
all modules of the application, or all modules to map successfully?



yes the issue is very likely that more modules are being imported
within non-main threads, and more mappers are coming in.   if you get
absolutely every mapper loaded up, then call compile_mappers() before
any threads spawn, this kind of problem shouldn't occur.


Hm, should this include the dynamic mappers? These mappers are run with 
/request/ specific parameters - if a user requests to see data of a 
certain view, a select object (using the current view parameter value) 
and a related class are generated, used and released. These mappers 
/will/ come in with threads. So, if absolutely every mapper includes 
the mappers to selectables this cannot be achieved I fear ...


Until now, I understood we should run all our standard mappers (to 
tables), finally run compile_mappers(), and could start the request 
handler cycle (with threaded handlers and new mappers produced on 
request) afterwards.



There may be other bugs in mapper configuration in 0.5 though this
seems quite unusual that you're getting threading errors this
frequently (especially dictionary changed size  ?  stack trace on 
that

?) .


I will try to produce a stack trace. (In the last instrumented debug 
versions, I caught all exceptions so the stack traces were not logged.)



Trying 0.6 at least might be worth it.


Just in case I could not describe clearly before how dynamic and 
request specific the function generated mappings are: would 0.6 allow us 
to use the pattern of dynamic mapping described above?


Thanks and regards

 Jochen



--
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: How can I use and_ and or_ in the orm query ?

2012-05-11 Thread Jonathan Vanasco
it's not in the api docs though - which tends to come up first on the
keyword search and is the more obvious place to look.

http://docs.sqlalchemy.org/en/rel_0_7/orm/query.html#sqlalchemy.orm.query.Query.filter

http://docs.sqlalchemy.org/en/rel_0_7/core/expression_api.html?highlight=and_#sqlalchemy.sql.expression.and_

http://docs.sqlalchemy.org/en/rel_0_7/core/expression_api.html?highlight=or_#sqlalchemy.sql.expression.or_

Just saying/linking something like:

filter:
This also accepts clauses created by the [and_] and [or_]
functions.

and_:
or_:
The result of this can also be used by the [filter] function.

-- 
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.