[sqlalchemy] Re: Session().execute(...) with parameter syntax error exception

2007-12-26 Thread Michael Bayer


On Dec 25, 2007, at 2:54 PM, jerryji wrote:


 However, the following parameterized version fails --

 result = model.Session.execute(select * from labels where
 labelid=:labelid, {'labelid':10}, mapper=model.Label)

 with the following syntax error exception --

 ...

heya -

the generic textual bind params i.e. :someparam apply to the  
sql.text() construct, whereas a plain string doesnt get processed at  
all, so do it like Session.execute(text(select * from foo where  
x=:label), {params}, ...).

- mike

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Objects are stored in DB when nobody asks to do so

2007-12-26 Thread Denis S. Otkidach

The following code fails on the last assert statement (SQLAlchemy
0.4.1):

---8---
from __future__ import with_statement

import sqlalchemy as sa, logging
from sqlalchemy.orm import mapper, sessionmaker

logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
logging.basicConfig()

class ModelObject(object):

def __init__(self, title):
self.title = title

metadata = sa.MetaData()

objectTable = sa.Table(
'Objects', metadata,
sa.Column('id', sa.Integer, primary_key=True, autoincrement=True),
sa.Column('title', sa.String(255), nullable=False),
)

objectsMapper = mapper(ModelObject, objectTable)

engine = sa.create_engine('sqlite://')
metadata.create_all(engine, checkfirst=True)

session = sessionmaker(bind=engine, autoflush=False,
transactional=False)()

obj1 = ModelObject(u'title-1.1')
with session.begin():
session.save(obj1)
# No session.flush() here. Is it OK? At least we did something to
save it.
obj1ID = obj1.id

session.clear()

obj1 = session.get(objectsMapper, obj1ID)
assert obj1.title==u'title-1.1'

obj1.title = u'title-1.2' # It's not intended to be saved

obj2 = ModelObject(u'title-2')
with session.begin():
session.save(obj2)
obj2ID = obj2.id

session.clear()

obj2 = session.get(objectsMapper, obj2ID)
assert obj2.title==u'title-2'

obj1 = session.get(objectsMapper, obj1ID)
assert not session.autoflush
assert obj1.title==u'title-1.1' # But we didn't save/flush it!
---8---

All modified objects are saved for each transaction and I see no way
to control this. Am I right? There is a lot of cases when such
behavior in unacceptable. Is it intended or a bug?

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Session().execute(...) with parameter syntax error exception

2007-12-26 Thread jerryji

Hi Michael,

Thank you very much for the enlightenment.

In this case, I think the SQLAlchemy 0.4 documentation needs a little
update: under section Using SQL Expressions with Sessions (http://
www.sqlalchemy.org/docs/04/session.html#unitofwork_sql) --

Session = sessionmaker(bind=engine, transactional=True)
sess = Session()
result = sess.execute(select * from table where id=:id, {'id':7})

Shouldn't the sess.execute(...) line become --

result = sess.execute(text(select * from table where id=:id), {'id':
7})

Thanks.

Jerry

On Dec 26, 9:56 am, Michael Bayer [EMAIL PROTECTED] wrote:
 On Dec 25, 2007, at 2:54 PM, jerryji wrote:



  However, the following parameterized version fails --

  result = model.Session.execute(select * from labels where
  labelid=:labelid, {'labelid':10}, mapper=model.Label)

  with the following syntax error exception --

  ...

 heya -

 the generic textual bind params i.e. :someparam apply to the
 sql.text() construct, whereas a plain string doesnt get processed at
 all, so do it like Session.execute(text(select * from foo where
 x=:label), {params}, ...).

 - mike
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Objects are stored in DB when nobody asks to do so

2007-12-26 Thread Michael Bayer



On Dec 26, 10:15 am, Denis S. Otkidach [EMAIL PROTECTED]
wrote:

 All modified objects are saved for each transaction and I see no way
 to control this. Am I right? There is a lot of cases when such
 behavior in unacceptable. Is it intended or a bug?

the way around this depends on what you're trying to do.  The code you
pasted is issuing a flush().  Is that what you intended to do ?  A
flush by default does write all pending changes within the session to
the database.  flush() does have the option to flush individual
objects, like this:

session.flush([obj2])

in the above case, you needn't begin/commit your own transaction the
way you're doing; flush() always does everything in its own
transaction if one is not established.  if you do use an actual begin/
commit, commit() does not have the same per-object option as flush and
implies a full flush() - the full contents of the session should be
considered a single transactional unit in most cases.

so, if you just want to remove obj1 from any further flushes, remove
it from the session using session.expunge(obj1).   you're also free to
use multiple sessions, one per each object or group of objects, so
that they may be independently managed.

yet another scenario, you want to use transactions that are
independent of session flushes.  To accomplish this, use engine- or
connection-level transactions, as described in the second half of
http://www.sqlalchemy.org/docs/04/session.html#unitofwork_sql .  in
this case you control the transactions independently of any session
behavior, yet the session still participates (at your choosing) in the
overall transaction.

Hope this helps.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Objects are stored in DB when nobody asks to do so

2007-12-26 Thread Denis S. Otkidach

On Dec 26, 2007 6:29 PM, Michael Bayer [EMAIL PROTECTED] wrote:
 yet another scenario, you want to use transactions that are
 independent of session flushes.  To accomplish this, use engine- or
 connection-level transactions, as described in the second half of
 http://www.sqlalchemy.org/docs/04/session.html#unitofwork_sql .  in
 this case you control the transactions independently of any session
 behavior, yet the session still participates (at your choosing) in the
 overall transaction.

 Hope this helps.

Sure, this is what I needed. Thanks! I believe SA documentation should
explicitly state that session.commit() always flushes all modified
objects independent on autoflush option.

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---