RE: [sqlalchemy] m2m introspection

2013-12-09 Thread Alexey Vihorev
Thanks!

Yes, it is documented, and I tried to sift through the URLs before asking,
but it's kind of difficult (at least for me) to understand where exactly
should I be looking for a particular piece of introspection info.

 

From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On
Behalf Of Michael Bayer
Sent: Sunday, December 08, 2013 10:47 PM
To: sqlalchemy@googlegroups.com
Subject: Re: [sqlalchemy] m2m introspection

 

with modern sqlalchemy do this:

 

from sqlalchemy import inspect

 

for rel in inspect(MyClass).relationships:

   target_class = rel.mapper.class_

 

this stuff is all documented, though it involves familiarizing with a few
basic constructs like Mapper and RelationshipProperty:

 

http://docs.sqlalchemy.org/en/rel_0_9/core/inspection.html?highlight=inspect
#sqlalchemy.inspection

http://docs.sqlalchemy.org/en/rel_0_9/orm/mapper_config.html?highlight=relat
ionships#sqlalchemy.orm.mapper.Mapper.relationships

http://docs.sqlalchemy.org/en/rel_0_9/orm/internals.html#sqlalchemy.orm.prop
erties.RelationshipProperty.mapper

http://docs.sqlalchemy.org/en/rel_0_9/orm/mapper_config.html#sqlalchemy.orm.
mapper.Mapper.class_

 

On Dec 8, 2013, at 5:55 AM, Alexey Vihorev viho...@gmail.com wrote:





Hi again!

I can do MyClass.iterate_properties()and check if a property is m2m
relationship. But how can I find out which class it references? Thanks!

 

-- 
You received this message because you are subscribed to the Google Groups
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an
email to mailto:sqlalchemy+unsubscr...@googlegroups.com
sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to  mailto:sqlalchemy@googlegroups.com
sqlalchemy@googlegroups.com.
Visit this group at  http://groups.google.com/group/sqlalchemy
http://groups.google.com/group/sqlalchemy.
For more options, visit  https://groups.google.com/groups/opt_out
https://groups.google.com/groups/opt_out.

 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


[sqlalchemy] multiple databases?

2013-12-09 Thread Richard Gerd Kuesters

hi all,

i don't know if anyone have to go through this, but here's a question: 
is it possible to use multiple databases sources (like postgres and 
mysql) in a single application?



my best regards,
richard.

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] multiple databases?

2013-12-09 Thread Richard Gerd Kuesters
ok, disconsider, i may use mike's session router: 
http://techspot.zzzeek.org/2012/01/11/django-style-database-routers-in-sqlalchemy/



On 12/09/2013 04:08 PM, Richard Gerd Kuesters wrote:

hi all,

i don't know if anyone have to go through this, but here's a question: 
is it possible to use multiple databases sources (like postgres and 
mysql) in a single application?



my best regards,
richard. --
You received this message because you are subscribed to the Google 
Groups sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to sqlalchemy+unsubscr...@googlegroups.com.

To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] multiple databases?

2013-12-09 Thread Warwick Prince
Hi Richard

There are no problems connecting to multiple database sources in the one 
application - we do it all the time :-)

Cheers
Warwick

On 10 Dec 2013, at 4:08 am, Richard Gerd Kuesters rich...@humantech.com.br 
wrote:

 hi all,
 
 i don't know if anyone have to go through this, but here's a question: is it 
 possible to use multiple databases sources (like postgres and mysql) in a 
 single application?
 
 
 my best regards,
 richard.
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] Merge(load=False) woes

2013-12-09 Thread Jonathan Rogers
On Wednesday, August 25, 2010 11:02:19 AM UTC-4, Michael Bayer wrote:


 On Aug 25, 2010, at 10:47 AM, Michael Bayer wrote:

  
  On Aug 25, 2010, at 9:37 AM, Nikolaj wrote:
  
  Hello,
  
  I'm struggling to use the Beaker caching example in my project.
  Accessing any attribute on an instance from cache triggers a load from
  database of the instance - even trying to read the primary key!
  
  that means the instances that you're putting in the cache are expired, 
 or at least those attributes you are attempting to read.The beaker 
 example takes the objects from a result and sends them straight to the 
 cache, but does not detach them from the current session.  Therefore, if 
 your backend is the memory backend, the objects in the cache are the same 
 as those in your current session and they'll get expired when the session 
 is committed or rolled back.   
  
  There's not a really spectacular way to get around that except to not 
 use the memory backend, use one that pickles like memcached (well, pretty 
 much memcached is the only backend I'd ever use, but the file/dbm file 
 backends would work here too).

 well, this probably would work, this diff is against tip:

 diff -r 568ef214c1ac examples/beaker_caching/caching_query.py
 --- a/examples/beaker_caching/caching_query.pyMon Aug 23 18:17:31 
 2010 -0400
 +++ b/examples/beaker_caching/caching_query.pyWed Aug 25 10:55:53 
 2010 -0400
 @@ -63,8 +63,15 @@
 if particular attributes have been configured.
 
  
 +
 +def createfunc():
 +items = list(Query.__iter__(self))
 +for item in items:
 +self.session.expunge(item)
 +return items
 +
  if hasattr(self, '_cache_parameters'):
 -return self.get_value(createfunc=lambda: 
 list(Query.__iter__(self)))
 +return self.get_value(createfunc=createfunc)
  else:
  return Query.__iter__(self)
  

 I've added a comment in the example regarding this.  


I want to cache mapped objects from rows which will not change during the 
process lifetime of a web application, so I used the memory backend. Based 
on the comment in the example from SQLAlchemy  0.7.9, I have also 
implemented this approach of caching detached objects. It mostly works 
fine. However, sometimes objects lazily loaded from a collection 
relationship and cached via RelationshipObject are still detached when the 
application code gets them. It looks like CachingQuery.get_value() should 
make sure any objects loaded from the cache are merged, but somehow that's 
not happening.

The web application is a single-threaded, long lived process which 
maintains one Session object for its entire lifetime, repeatedly committing 
or rolling back for each HTTP request. I'm only seeing the detached objects 
on the request to a given process so it seems that objects loaded from the 
database for inserting into the cache are staying detached but those 
fetched from the cache on subsequent requests are properly merged. Do you 
have any idea why this would be or where I should investigate?

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] Merge(load=False) woes

2013-12-09 Thread Michael Bayer

On Dec 9, 2013, at 11:20 PM, Jonathan Rogers jonathanrrog...@gmail.com wrote:

 
 I want to cache mapped objects from rows which will not change during the 
 process lifetime of a web application, so I used the memory backend. Based on 
 the comment in the example from SQLAlchemy  0.7.9, I have also implemented 
 this approach of caching detached objects. It mostly works fine. However, 
 sometimes objects lazily loaded from a collection relationship and cached via 
 RelationshipObject are still detached when the application code gets them. It 
 looks like CachingQuery.get_value() should make sure any objects loaded from 
 the cache are merged, but somehow that's not happening.


the object coming out of merge() shouldn’t have anything detached associated 
with it - it either merges along the relationship paths, or it doesn’t and 
those relationships come out unloaded.  is this something you can distill down 
to a test case ?



 
 The web application is a single-threaded, long lived process which maintains 
 one Session object for its entire lifetime, repeatedly committing or rolling 
 back for each HTTP request. I'm only seeing the detached objects on the 
 request to a given process so it seems that objects loaded from the database 
 for inserting into the cache are staying detached but those fetched from the 
 cache on subsequent requests are properly merged. Do you have any idea why 
 this would be or where I should investigate?
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email tosqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.



signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: [sqlalchemy] Merge(load=False) woes

2013-12-09 Thread Jonathan Rogers
Michael Bayer wrote:

 On Dec 9, 2013, at 11:20 PM, Jonathan Rogers
 jonathanrrog...@gmail.com mailto:jonathanrrog...@gmail.com wrote:


 I want to cache mapped objects from rows which will not change during
 the process lifetime of a web application, so I used the memory
 backend. Based on the comment in the example from SQLAlchemy  0.7.9,
 I have also implemented this approach of caching detached objects. It
 mostly works fine. However, sometimes objects lazily loaded from a
 collection relationship and cached via RelationshipObject are still
 detached when the application code gets them. It looks like
 CachingQuery.get_value() should make sure any objects loaded from the
 cache are merged, but somehow that's not happening.


 the object coming out of merge() shouldn’t have anything detached
 associated with it - it either merges along the relationship paths, or
 it doesn’t and those relationships come out unloaded.  is this
 something you can distill down to a test case ?   

Yeah, that's what I thought about merge. The behavior I've observed is
intimately tied to my application so I'll try to come up with a test
case based on just the Beaker caching example.

-- 
Jonathan Rogers

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


[sqlalchemy] No operator matches the given name and argument filtering on postgres array

2013-12-09 Thread Matthew Pounsett
I'm trying to work with the postgres ARRAY type and I'm having a hard time 
figuring out what I'm doing wrong with filtering queries on the array 
column.  Here's some sample code, omitting the session setup:

class MyTable(Base):   
 
   __tablename__ = 'mytable'
   id = Column(Integer, primary_key=True)   
   
   myset = Column(sqlalchemy.dialects.postgresql.ARRAY(String)) 
   

Base.metadata.create_all(engine)   
 


z = ['a', 'b', 'c'] 


match = session.query(MyTable).\   
 
filter(MyTable.myset == z).\   
 
all()   




Note that the table is completely empty at this point, and has just been 
created by the create_all() method.  I expect to get back an empty 'match' 
list, but when the query runs I get a long stack trace and the following 
sql error:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) operator does not 
exist: character varying[] = text[]
LINE 3: WHERE mytable.myset = ARRAY[E'a', E'b', E'c']
^
HINT:  No operator matches the given name and argument type(s). You might 
need to add explicit type casts.
 'SELECT mytable.id AS mytable_id, mytable.myset AS mytable_myset \nFROM 
mytable \nWHERE mytable.myset = %(myset_1)s' {'myset_1': ['a', 'b', 'c']}

On the off chance it mattered, I've tried setting z to be a tuple instead 
of a list, but no joy there either.  What am I misunderstanding about how 
the array type works?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] No operator matches the given name and argument filtering on postgres array

2013-12-09 Thread Michael Bayer


On Dec 10, 2013, at 12:00 AM, Matthew Pounsett matt.pouns...@gmail.com wrote:

 I'm trying to work with the postgres ARRAY type and I'm having a hard time 
 figuring out what I'm doing wrong with filtering queries on the array column. 
  Here's some sample code, omitting the session setup:
 
 class MyTable(Base):  
   
__tablename__ = 'mytable'
id = Column(Integer, primary_key=True) 
  
myset = Column(sqlalchemy.dialects.postgresql.ARRAY(String))   
  
 
 Base.metadata.create_all(engine)  
   
   
   
 z = ['a', 'b', 'c']   
   
 
 match = session.query(MyTable).\  
   
 filter(MyTable.myset == z).\  
   
 all() 
   
 

that’s a little strange but you can get around it using CAST:

match = session.query(MyTable).\
filter(MyTable.myset == cast(z, ARRAY(String))).\
all()

though maybe there’s a better operator to use for array == array comparison, 
not sure


of course if you declare you column like this:

   myset = Column(ARRAY(Text))


it works…..in Postgresql there’s no real difference between TEXT and VARCHAR 
(which is unusual).   Not sure why PG can’t cast between array of VARCHAR vs. 
TEXT but I bet there’s some info on the web about that, it’s unusual PG would 
have a problem with something like that (though a google seems to confirm it).




signature.asc
Description: Message signed with OpenPGP using GPGMail