func.now() does not work with multi-inserts
Issue #1748
resolved
If I use the following code execute inserts one at a time where one of the values is func.now(), I get the expected result:
qi_sess_t = table.pl_QueryInvocation_Session
for qi_pg in queryInvocationsAndPages:
if ( not isinstance( qi_pg, tuple ) ):
continue
stmt = qi_sess_t.insert().values( {
'queryInvocation_id': qi_pg[ 0 ].id,
'session_id': session.id,
'createdAt': func.now(),
} )
session.bind.echo = True
session.execute( stmt )
session.bind.echo = False
Output:
2010-03-25 20:57:53,923: INFO sqlalchemy.engine.base.Engine.0x...bccL INSERT INTO `pl_QueryInvocation_Session` (`queryInvocation_id`, session_id, `createdAt`) VALUES (%s, %s, now())
2010-03-25 20:57:53,924: INFO sqlalchemy.engine.base.Engine.0x...bccL [3211L](30076L,)
However, if I try to use the executemany form, func.now() doesn't seem to be handled (compiled?) correctly:
qi_sess_t = table.pl_QueryInvocation_Session
stmt = qi_sess_t.insert()
values = [ 'queryInvocation_id': qi_pg[ 0 ]({
).id,
'session_id': session.id,
'createdAt': func.now(),
}
for qi_pg in queryInvocationsAndPages
if ( isinstance( qi_pg, tuple ) )
]
session.bind.echo = True
session.execute( stmt, values )
session.bind.echo = False
Output:
2010-03-25 20:57:53,925: INFO sqlalchemy.engine.base.Engine.0x...bccL INSERT INTO `pl_QueryInvocation_Session` (`queryInvocation_id`, session_id, `createdAt`) VALUES (%s, %s, %s)
2010-03-25 20:57:53,926: INFO sqlalchemy.engine.base.Engine.0x...bccL [3211L, <sqlalchemy.sql.functions.now at 0xb673f6ac; now>]([30061L,), [3211L, <sqlalchemy.sql.functions.now at 0xb673ff4c; now>](30062L,), [3211L, <sqlalchemy.sql.functions.now at 0xb673f0ac; now>](30063L,), [3211L, <sqlalchemy.sql.functions.now at 0xb673f4ec; now>](30064L,), [3211L, <sqlalchemy.sql.functions.now at 0xb6706c4c; now>](30065L,), [3211L, <sqlalchemy.sql.functions.now at 0xb6706bac; now>](30066L,), [3211L, <sqlalchemy.sql.functions.now at 0xb67060cc; now>](30067L,), [3211L, <sqlalchemy.sql.functions.now at 0xb670696c; now>](30068L,), [3211L, <sqlalchemy.sql.functions.now at 0xb670672c; now>](30069L,), [3211L, <sqlalchemy.sql.functions.now at 0xb6742c2c; now>](30070L,), [3211L, <sqlalchemy.sql.functions.now at 0xb674246c; now>](30071L,), [3211L, <sqlalchemy.sql.functions.now at 0xb67428ac; now>](30072L,), [3211L, <sqlalchemy.sql.functions.now at 0xb6759e6c; now>](30073L,), [3211L, <sqlalchemy.sql.functions.now at 0xb67591ac; now>](30074L,), [3211L, <sqlalchemy.sql.functions.now at 0xb670f64c; now>](30075L,), [3211L, <sqlalchemy.sql.functions.now at 0xb670fb8c; now>](30076L,)]
2010-03-25 20:57:53,926: INFO sqlalchemy.engine.base.Engine.0x...bccL [3211L, <sqlalchemy.sql.functions.now at 0xb673f6ac; now>]([30061L,), [3211L, <sqlalchemy.sql.functions.now at 0xb673ff4c; now>](30062L,), [3211L, <sqlalchemy.sql.functions.now at 0xb673f0ac; now>](30063L,), [3211L, <sqlalchemy.sql.functions.now at 0xb673f4ec; now>](30064L,), [3211L, <sqlalchemy.sql.functions.now at 0xb6706c4c; now>](30065L,), [3211L, <sqlalchemy.sql.functions.now at 0xb6706bac; now>](30066L,), [3211L, <sqlalchemy.sql.functions.now at 0xb67060cc; now>](30067L,), [3211L, <sqlalchemy.sql.functions.now at 0xb670696c; now>](30068L,), [3211L, <sqlalchemy.sql.functions.now at 0xb670672c; now>](30069L,), [3211L, <sqlalchemy.sql.functions.now at 0xb6742c2c; now>](30070L,), [3211L, <sqlalchemy.sql.functions.now at 0xb674246c; now>](30071L,), [3211L, <sqlalchemy.sql.functions.now at 0xb67428ac; now>](30072L,), [3211L, <sqlalchemy.sql.functions.now at 0xb6759e6c; now>](30073L,), [3211L, <sqlalchemy.sql.functions.now at 0xb67591ac; now>](30074L,), [3211L, <sqlalchemy.sql.functions.now at 0xb670f64c; now>](30075L,), [3211L, <sqlalchemy.sql.functions.now at 0xb670fb8c; now>](30076L,)]
/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-linux-i686.egg/MySQLdb/cursors.py:89: DeprecationWarning: functions overriding warnings.showwarning() must support the 'line' argument
/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-linux-i686.egg/MySQLdb/cursors.py:205: Warning: Data truncated for column 'createdAt' at row 1
I'm using python 2.6, sqlalchemy 0.5.6, and mysql 5.1
Comments (2)
-
Account Deleted -
repo owner - changed status to wontfix
Hi there -
the "func.now()" expression is not a bind value, it must be present within the statement itself.
correct usage:
stmt = table.insert().values(createdAt=func.now()) values = [ {'queryid':queryid, 'sessionid':sessionid} for queryid, sessionid in collection ]( ) session.execute(stmt, values)
hope this helps.
- Log in to comment
Oops, please ignore the second line of the second output - meant to clean that up.