Re: [sqlalchemy] Handling detached instances

2014-10-01 Thread Wichert Akkerman

On 30 Sep 2014, at 23:18, tonthon tontho...@gmail.com wrote:
 I didn't knew merge was supposes to be used in such a case, it works like a 
 charm.
 Following that tip, I've added this decorator :
 
  def cache_wrapper(func):   
   ensure a model returned from a cached function is attached to the 
  current session  
  def cached_func_wrapper(*args, **kwargs): 

  obj = func(*args, **kwargs)   

  if object_session(obj) is None and has_identity(obj): 

  obj = DBSESSION().merge(obj)  

  return obj

  return cached_func_wrapper  

Keep in mind that merge can still hit your SQL database to refresh and 
attributes. If you know your cache is not stale you will want to use the 
load=False parameter for merge() to prevent that from happening.

Wichert.

-- 
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/d/optout.


[sqlalchemy] UNION howto

2014-10-01 Thread Jose Soares

Hi all,

Could someone help me to define in sqlalchemy the following query:

sql=SELECT count(*)  FROM
(SELECT cod_sticker AS bruciato FROM scadenziario
   UNION SELECT cod_sticker AS bruciato FROM sopralluogo
   UNION SELECT sticker_checklist AS bruciato FROM sopralluogo
   UNION SELECT protocollo AS bruciato FROM prestazione) AS foo
   WHERE bruciato='E1212';

thank for any help.
j

--
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/d/optout.


[sqlalchemy] Re: UNION howto

2014-10-01 Thread Jonathan Vanasco
I asked a similar question a few days ago; it's still on the front page of 
the group.  Mike replied to that and gave some details

Your query would be something like:




class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
cod_sticker = Column(String)
sticker_checklist = Column(String)
protocollo = Column(String)

q1 = sess.query(A.cod_sticker.label('bruciato'))
q2 = sess.query(A.cod_sticker.label('bruciato'))
q3 = sess.query(A.sticker_checklist.label('bruciato'))
q4 = sess.query(A.protocollo.label('bruciato'))

as_union = union(q1, q2, q3, q4)
as_union_alias = as_union.alias('foo')

q = sess.query(as_union_alias).filter( as_union_alias.c.bruciato == 
'E1212').count()

-- 
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/d/optout.


[sqlalchemy] statement.compile and legacy is not null code

2014-10-01 Thread Jonathan Vanasco
I'm just posting this to the group for search equity--

Until recently (2012, via this commit 
https://bitbucket.org/zzzeek/sqlalchemy/issue/2544), the way recommended 
several times in this group and stackoverflow to generate IS NOT NULL was 
to use a column opreation:

sq = sess.query(A.id.label('id')).filter(A.id.op('IS NOT')(None))

However, that will cause a sqlalchemy.exc.CompileError when print/compiling 
a statement with literal binds

# this will raise an error
print str(sq.statement.compile(dialect=postgresql.dialect(), 
compile_kwargs={literal_binds: True}))

To get around that issue, these forms will all compile correctly:

  # 0.7.9 and later
  sq = sess.query(A.id.label('id')).filter(A.id != None)

  # 0.7.8 and earlier
  sq = sess.query(A.id.label('id')).filter(A.id.op('IS 
NOT')(sqlalchemy.sql.expression.null()))

-- 
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/d/optout.


Re: [sqlalchemy] statement.compile and legacy is not null code

2014-10-01 Thread Claudio Freire
On Wed, Oct 1, 2014 at 6:51 PM, Jonathan Vanasco jvana...@gmail.com wrote:

   # 0.7.9 and later
   sq = sess.query(A.id.label('id')).filter(A.id != None)


I've used ~(id == None) since 0.3 quite successfully (though I've
eventually switched to != None since it's way more natural so not sure
if there's any place where it doesn't compile well)

-- 
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/d/optout.


Re: [sqlalchemy] statement.compile and legacy is not null code

2014-10-01 Thread Michael Bayer

you have either x != None or x != null() or x.isnot(None), or 
x.isnot(null()))

a lot of choices!   



On Oct 1, 2014, at 5:51 PM, Jonathan Vanasco jvana...@gmail.com wrote:

 I'm just posting this to the group for search equity--
 
 Until recently (2012, via this commit 
 https://bitbucket.org/zzzeek/sqlalchemy/issue/2544), the way recommended 
 several times in this group and stackoverflow to generate IS NOT NULL was 
 to use a column opreation:
 
 sq = sess.query(A.id.label('id')).filter(A.id.op('IS NOT')(None))
 
 However, that will cause a sqlalchemy.exc.CompileError when print/compiling a 
 statement with literal binds
 
 # this will raise an error
 print str(sq.statement.compile(dialect=postgresql.dialect(), 
 compile_kwargs={literal_binds: True}))
 
 To get around that issue, these forms will all compile correctly:
 
   # 0.7.9 and later
   sq = sess.query(A.id.label('id')).filter(A.id != None)
 
   # 0.7.8 and earlier
   sq = sess.query(A.id.label('id')).filter(A.id.op('IS 
 NOT')(sqlalchemy.sql.expression.null()))
 
 
 -- 
 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/d/optout.

-- 
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/d/optout.