[sqlalchemy] @propert
I add a python property to my class, can i make this query able? Anyone knows of a good sample on how to do this, I would really appreciate. rgds, Paulo --~--~-~--~~~---~--~~ 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] Dynamic loader versus lazy=True
Hi all, I observed that if I define a relation (foo_query) as lazy='dynamic' and access all referenced entities with foo_query.all() that the query will be executed every time i access it. That is not a big surprise ;-) In a library I am writing i want to provide methods that allow pre-filtering of referenced entities and also on that provides access to all entities. I am wondering if it is better/faster/.. to define *two* relations for filtering and accessing all entities respectively. I can't really decide between the following two approaches and would be happy if someone could provide some tips: Approach 1 -- Class Bar(object): def all_foo(self): foo_query.all() def foo_startwith(self, search_string): foo.query.filter(tbl.c.col.like('%s%%'% ...)) mapper(Bar, ... properties={ 'foo_query': relation(Foo, lazy='dynamic') }) Approach 2 -- Class Bar(object): def foo_startwith(self, search_string): foo.query.filter(tbl.c.col.like('%s%%'% ...)) mapper(Bar, ... properties={ 'all_foo': relation(Foo) }) properties={ 'foo_query': relation(Foo, lazy='dynamic') }) Which one is faster? Does it make a difference, given the optimisation/cache in the database? Will it just mean more bloat in the mapper definition? thanks Wolodja Wentland signature.asc Description: Digital signature
[sqlalchemy] Re: finding related objects
Martijn Faassen wrote: > > Hey, > > [finding related objects] > > One way I've come up with is this: > > > m = object_mapper(model) > for prop in m.iterate_properties: > if isinstance(prop, RelationProperty): > if prop.direction is ONETOMANY: > for related in getattr(model, prop.key): > do_stuff(related) > > the other directions still need to be handled though (and recursion). > > Would this be a reasonable solution? it is. cascade_iterator() is on the right track too, the string argument you'd send would be a "cascade" type, one of "save-update", "merge", "delete", etc. "all" is one of the arguments for "cascade" its true but internally that is expanded into the individual entries (see CascadeOptions in sqlalchemy.orm.util). > > Regards, > > Martijn > > > > > --~--~-~--~~~---~--~~ 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: finding related objects
Hey, [finding related objects] One way I've come up with is this: m = object_mapper(model) for prop in m.iterate_properties: if isinstance(prop, RelationProperty): if prop.direction is ONETOMANY: for related in getattr(model, prop.key): do_stuff(related) the other directions still need to be handled though (and recursion). Would this be a reasonable solution? Regards, Martijn --~--~-~--~~~---~--~~ 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: Threading
Michael Mileusnich wrote: > I have a db file with a function called getsession() that returns my > Session > which has been established to = a scoped session. The odd thing is it > works > fine on my Windows XP machine, however on my ubuntu box it throws errors. > I > guess I'll reply later today with the errors. > Thanks > Michael Mileusnich keep in mind that DBAPI connections are usually not going to persist across a fork() on a unix system, so that each time you start a new child process, you have to create a new engine and ensure that the engine of the parent process is no longer accessed. > > On Thu, Sep 10, 2009 at 10:10 AM, Michael Bayer > wrote: > >> >> Michael Mileusnich wrote: >> > I have switched my application to use the Python multiprocessor >> threading >> > instead if the default python gil threading. I seem to be running >> into >> > issues. I am using scoped sessions and in my run() function, when I >> try >> > to >> > do sess = getsession() to grab the session sql alchemy throws a large >> > number >> > of errors. Is this supported? >> >> I'm assuming you're referring to the "multiprocessing" module in Python >> 2.6. It is absolutely supported, I run several applications in a >> production environment with heavy usage of multiprocessing. >> >> >> > >> > > > > --~--~-~--~~~---~--~~ 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: Threading
I have a db file with a function called getsession() that returns my Session which has been established to = a scoped session. The odd thing is it works fine on my Windows XP machine, however on my ubuntu box it throws errors. I guess I'll reply later today with the errors. Thanks Michael Mileusnich On Thu, Sep 10, 2009 at 10:10 AM, Michael Bayer wrote: > > Michael Mileusnich wrote: > > I have switched my application to use the Python multiprocessor threading > > instead if the default python gil threading. I seem to be running into > > issues. I am using scoped sessions and in my run() function, when I try > > to > > do sess = getsession() to grab the session sql alchemy throws a large > > number > > of errors. Is this supported? > > I'm assuming you're referring to the "multiprocessing" module in Python > 2.6. It is absolutely supported, I run several applications in a > production environment with heavy usage of multiprocessing. > > > > > --~--~-~--~~~---~--~~ 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: Threading
Michael Mileusnich wrote: > I have switched my application to use the Python multiprocessor threading > instead if the default python gil threading. I seem to be running into > issues. I am using scoped sessions and in my run() function, when I try > to > do sess = getsession() to grab the session sql alchemy throws a large > number > of errors. Is this supported? I'm assuming you're referring to the "multiprocessing" module in Python 2.6. It is absolutely supported, I run several applications in a production environment with heavy usage of multiprocessing. --~--~-~--~~~---~--~~ 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] finding related objects
Hi there, I'm trying to figure out whether there's something in SQLAlchemy that lets me reliably get all the objects related to another: i.e. all children of a parent that (in the underlying record) have a foreign key relationship to the parent record. One way I've been trying to solve this is by inspecting the underlying tables directly, inspecting the foreign_keys property of the tables. But this is mixing levels however - I get into trouble getting from ORMed object to record and back again (having to expunge the session, etc). I was wondering whether there is something in the ORM that can help me here. It tricks me that the cascading behavior might be close to what I'm looking for, but I'm not sure. I've tried to use the cascade_iterator on the mapper but I'm not sure I'm using it right (as I get no related objects while I expect some, or an error). If there is a way to get the related objects (in the session or not; I need all of them) in the ORM layer, could someone post me a snippet? I've been trying something like: foo = m = object_mapper(foo) related = list(m.cascade_iterator('all', foo)) I'm not sure whether I'm passing the right arguments or whether the cascade story is even suitable for that. I just get an empty list, even though foo does have several objects that refer to it. Any ideas? Regards, Martijn --~--~-~--~~~---~--~~ 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] Threading
I have switched my application to use the Python multiprocessor threading instead if the default python gil threading. I seem to be running into issues. I am using scoped sessions and in my run() function, when I try to do sess = getsession() to grab the session sql alchemy throws a large number of errors. Is this supported? --~--~-~--~~~---~--~~ 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] ORM and EXISTS?
Hi! I'm having trouble getting a query to work. The query looks rather simple but I can't manage to build it using sqlalchemy - especially I didn't find a way for the exists clause to build. The query (simplified): SELECT * from ATable WHERE EXISTS ( SELECT 1 FROM BTable join CTable on BTable.CTable_id = CTable.id WHERE CTable.cval IN ('foo', 'bar') AND BTable.ATable_id = ATable.id ) The Model (simplified): class ATable(Base): id = Column(Integer, primary_key=True) aval = Column(Unicode(80)) class BTable(Base): id = Column(Integer, primary_key=True) atable_id = Column(ForeignKey(ATable.id), index=True) ctable_id = Column(ForeignKey(CTable.id), index=True) bval = Column(Unicode(80)) class CTable(Base): id = Column(Integer, primary_key=True) cval = Column(Unicode(80)) I tried a lot to get this query to work but nothing worked so far. Optimistically I thought that something simple as ex = session.query(BTable).join(CTable).filter(CTable.cval.in_(('foo', 'bar'))) q = session.query(ATable).filter(exists(ex)) could work "automagically" but that was unfortunately not the case. Can anyone help me on this? Thanks, seppo --~--~-~--~~~---~--~~ 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: Update session extension after creation
As expected, the simple test works. Something else is happening. In [7]: cpaste Pasting code; enter '--' alone on the line to stop. :Base=declarative_base() :Base.metadata.bind = create_engine('sqlite:///', echo=True) :class Foo(Base): :__tablename__ = 'foo' :id = Column(Integer, primary_key=True) :Base.metadata.create_all() :class MyExtension(SessionExtension): :def after_flush(self, session, context): :print '*** MyExtension.after_flush()' :Session = scoped_session(sessionmaker()) :session = Session() :print 'extensions before append', session.extensions :session.extensions.append(MyExtension()) :print 'extensions after append', session.extensions :session.add(Foo()) :session.flush() :-- 2009-09-10 09:52:09,328 INFO sqlalchemy.engine.base.Engine.0x...43b0 PRAGMA table_info("foo") 2009-09-10 09:52:09,328 INFO sqlalchemy.engine.base.Engine.0x...43b0 () 2009-09-10 09:52:09,342 INFO sqlalchemy.engine.base.Engine.0x...43b0 CREATE TABLE foo ( id INTEGER NOT NULL, PRIMARY KEY (id) ) 2009-09-10 09:52:09,375 INFO sqlalchemy.engine.base.Engine.0x...43b0 () 2009-09-10 09:52:09,375 INFO sqlalchemy.engine.base.Engine.0x...43b0 COMMIT extensions before append [] extensions after append [<__main__.MyExtension object at 0x025C48D0>] 2009-09-10 09:52:09,405 INFO sqlalchemy.engine.base.Engine.0x...43b0 BEGIN 2009-09-10 09:52:09,405 INFO sqlalchemy.engine.base.Engine.0x...43b0 INSERT INTO foo DEFAULT VALUES 2009-09-10 09:52:09,405 INFO sqlalchemy.engine.base.Engine.0x...43b0 [] *** MyExtension.after_flush() --~--~-~--~~~---~--~~ 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: Update session extension after creation
Now we are getting into areas beyond my SA understanding. Here is my test with scoped_session that shows in version 0.5.5 that session.extension is available and modifiable, don't know if it gets called but that would be pretty easy to check. You might want to try the same test. >>> import sqlalchemy >>> print sqlalchemy.__version__ 0.5.5 >>> >>> from sqlalchemy.orm import sessionmaker, scoped_session, SessionExtension >>> class MyExtension(SessionExtension): pass ... >>> Session = scoped_session(sessionmaker(extension=MyExtension())) >>> session = Session() >>> print session.extensions [<__main__.MyExtension object at 0x00FED090>] >>> >>> Session = scoped_session(sessionmaker()) >>> session = Session() >>> print session.extensions [] >>> session.extensions = [MyExtension()] >>> print session.extensions [<__main__.MyExtension object at 0x00FF50D0>] >>> >>> Session = scoped_session(sessionmaker()) >>> session = Session() >>> print session.extensions [] >>> session.extensions.append(MyExtension()) >>> print session.extensions [<__main__.MyExtension object at 0x01000130>] >>> -- 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] Re: Update session extension after creation
Hi, Thank you for your interest. Testing with this kind of declaration : session.extensions = [VersionedListener()] doesn't change anything and using this kind of declaration : session.extensions.append(MySessionExtension()) returns: AttributeError: 'ScopedSession' object has no attribute 'extensions' => Sounds like my scoped session doesn't have any extension at all (even empty) Regards, Laurent On 10 sep, 14:09, Mike Conley wrote: > Well, it looks like configure is a class method on Session, so when you do > session.configure() you are configuring future sessions, not the current > one. > > The extensions for a session instance are are a property named extensions. > You could try setting that list yourself. > > session.extensions = [MySessionExtension()] > > to replace the existing extension or > > session.extensions.append(MySessionExtension()) > > to add yours to the list > > But understand the risk that this is modifying internals and might not work, > and no guarantee it will work in future versions. > > -- > Mike Conley > > On Thu, Sep 10, 2009 at 4:29 AM, asrenzo wrote: > > > I also tried session.configure(extension=MySessionExtension()) with no > > success > > > On 10 sep, 10:13, Laurent Rahuel wrote: > > > Hi, > > > > I'm currently using a webframework which uses sqlalchemy. Each time a > > > request hits the server, the framework creates a session object I can > > > use during the page creation. > > > > I wish you could update this session with one of my SessionExtension but > > > I'm facing a small problem: > > > > I tested my code with a standalone session I had created with this > > > syntax : session = create_session(extension=MySessionExtension()) and > > > everything works. When I try the same code in my web context and I do: > > > > " > > > from framework.db import session > > > > session.extension = MySessionExtension() > > > > . > > > . > > > . > > > > """ > > > > None of my extension session methods are called. > > > > Any idea ? --~--~-~--~~~---~--~~ 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: Update session extension after creation
Well, it looks like configure is a class method on Session, so when you do session.configure() you are configuring future sessions, not the current one. The extensions for a session instance are are a property named extensions. You could try setting that list yourself. session.extensions = [MySessionExtension()] to replace the existing extension or session.extensions.append(MySessionExtension()) to add yours to the list But understand the risk that this is modifying internals and might not work, and no guarantee it will work in future versions. -- Mike Conley On Thu, Sep 10, 2009 at 4:29 AM, asrenzo wrote: > > I also tried session.configure(extension=MySessionExtension()) with no > success > > > On 10 sep, 10:13, Laurent Rahuel wrote: > > Hi, > > > > I'm currently using a webframework which uses sqlalchemy. Each time a > > request hits the server, the framework creates a session object I can > > use during the page creation. > > > > I wish you could update this session with one of my SessionExtension but > > I'm facing a small problem: > > > > I tested my code with a standalone session I had created with this > > syntax : session = create_session(extension=MySessionExtension()) and > > everything works. When I try the same code in my web context and I do: > > > > " > > from framework.db import session > > > > session.extension = MySessionExtension() > > > > . > > . > > . > > > > """ > > > > None of my extension session methods are called. > > > > Any idea ? > > > --~--~-~--~~~---~--~~ 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: Update session extension after creation
Small type : print session.is_active On 10 sep, 14:07, Laurent Rahuel wrote: > Here is a sample code: > > print session.is_active() > > class MySessionExtension(SessionExtension): > print "Entering MySessionExtension" > def before_flush(self, session, flush_context, instances): > print "Entering method before_flush" > > session.configure(extension=MySessionExtension()) > > # some data manipulation goes here with session.add(), session.delete > () > > session.flush() > session.commit() > > My session is active, I can see the trace "Entering > MySessionExtension" but I never see any "Entering method before_flush" > even if my objects are well stored or deleted in the database. > I use declarative and SA 0.5.5 > > Any clue ? > > Regards, > > Laurent > > On 10 sep, 10:29, asrenzo wrote: > > > I also tried session.configure(extension=MySessionExtension()) with no > > success > > > On 10 sep, 10:13, Laurent Rahuel wrote: > > > > Hi, > > > > I'm currently using a webframework which uses sqlalchemy. Each time a > > > request hits the server, the framework creates a session object I can > > > use during the page creation. > > > > I wish you could update this session with one of my SessionExtension but > > > I'm facing a small problem: > > > > I tested my code with a standalone session I had created with this > > > syntax : session = create_session(extension=MySessionExtension()) and > > > everything works. When I try the same code in my web context and I do: > > > > " > > > from framework.db import session > > > > session.extension = MySessionExtension() > > > > . > > > . > > > . > > > > """ > > > > None of my extension session methods are called. > > > > Any idea ? --~--~-~--~~~---~--~~ 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: Update session extension after creation
Here is a sample code: print session.is_active() class MySessionExtension(SessionExtension): print "Entering MySessionExtension" def before_flush(self, session, flush_context, instances): print "Entering method before_flush" session.configure(extension=MySessionExtension()) # some data manipulation goes here with session.add(), session.delete () session.flush() session.commit() My session is active, I can see the trace "Entering MySessionExtension" but I never see any "Entering method before_flush" even if my objects are well stored or deleted in the database. I use declarative and SA 0.5.5 Any clue ? Regards, Laurent On 10 sep, 10:29, asrenzo wrote: > I also tried session.configure(extension=MySessionExtension()) with no > success > > On 10 sep, 10:13, Laurent Rahuel wrote: > > > Hi, > > > I'm currently using a webframework which uses sqlalchemy. Each time a > > request hits the server, the framework creates a session object I can > > use during the page creation. > > > I wish you could update this session with one of my SessionExtension but > > I'm facing a small problem: > > > I tested my code with a standalone session I had created with this > > syntax : session = create_session(extension=MySessionExtension()) and > > everything works. When I try the same code in my web context and I do: > > > " > > from framework.db import session > > > session.extension = MySessionExtension() > > > . > > . > > . > > > """ > > > None of my extension session methods are called. > > > Any idea ? --~--~-~--~~~---~--~~ 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: Update session extension after creation
I also tried session.configure(extension=MySessionExtension()) with no success On 10 sep, 10:13, Laurent Rahuel wrote: > Hi, > > I'm currently using a webframework which uses sqlalchemy. Each time a > request hits the server, the framework creates a session object I can > use during the page creation. > > I wish you could update this session with one of my SessionExtension but > I'm facing a small problem: > > I tested my code with a standalone session I had created with this > syntax : session = create_session(extension=MySessionExtension()) and > everything works. When I try the same code in my web context and I do: > > " > from framework.db import session > > session.extension = MySessionExtension() > > . > . > . > > """ > > None of my extension session methods are called. > > Any idea ? --~--~-~--~~~---~--~~ 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] Update session extension after creation
Hi, I'm currently using a webframework which uses sqlalchemy. Each time a request hits the server, the framework creates a session object I can use during the page creation. I wish you could update this session with one of my SessionExtension but I'm facing a small problem: I tested my code with a standalone session I had created with this syntax : session = create_session(extension=MySessionExtension()) and everything works. When I try the same code in my web context and I do: " from framework.db import session session.extension = MySessionExtension() . . . """ None of my extension session methods are called. Any idea ? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---