I have a fairly standard SQLA setup with the exception of reflecting the table I'm using. Here's the code:
import sqlalchemy as sa
from sqlalchemy import orm
engine = sa.create_engine('mssql://<clip>')
metadata = sa.MetaData(bind=engine)
Session = orm.sessionmaker(autoflush=False, autocommit=False)
event_table = sa.Table('XML_INTERFACE', metadata, autoload=True)
class Event(object):
pass
orm.mapper(Event, event_table)
Engine connects to SQL Server 2005 or 2008 (I have one of both). It works as expected:
>>> session = Session()
>>> ev = session.query(Event).first()
INFO sqlalchemy.engine.base.Engine.0x...cd10 BEGIN
INFO sqlalchemy.engine.base.Engine.0x...cd10 SELECT TOP 1 [XML_INTERFACE] ...
INFO sqlalchemy.engine.base.Engine.0x...cd10 {}
>>> ev.TYOID = 1234
>>> session.commit()
INFO sqlalchemy.engine.base.Engine.0x...dd10 UPDATE [XML_INTERFACE] SET [TYOID]=%(TYOID)s WHERE [XML_INTERFACE].[ID] = %(XML_INTERFACE_ID)s
INFO sqlalchemy.engine.base.Engine.0x...dd10 {'TYOID': 1234, 'XML_INTERFACE_ID': 1}
INFO sqlalchemy.engine.base.Engine.0x...dd10 COMMIT
INFO sqlalchemy.engine.base.Engine.0x...dd10 BEGIN
INFO sqlalchemy.engine.base.Engine.0x...dd10 SELECT [XML_INTERFACE] … WHERE [XML_INTERFACE].[ID] = %(param_1)s
INFO sqlalchemy.engine.base.Engine.0x...dd10 {'param_1': 1}
But when trying different cases with wrong or corrupted data, I saw this. I expected to get conversion error from pushing string to Integer column (TYOID), but instead I get some weird rollback error.
>>> ev.TYOID = 'foobar'
>>> session.commit()
INFO sqlalchemy.engine.base.Engine.0x...cd10 UPDATE [XML_INTERFACE] ...
INFO sqlalchemy.engine.base.Engine.0x...cd10 {'TYOID': 'foobar', 'XML_INTERFACE_ID': 1}
INFO sqlalchemy.engine.base.Engine.0x...cd10 ROLLBACK
sqlalchemy.exc.OperationalError: (OperationalError) cannot roll back transaction: SQL Server message 3903, severity 16, state 1, line 1:
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
DB-Lib error message 3903, severity 16:
General SQL Server error: Check messages from the SQL Server
None None
Transactions are started whenever Session object is created, so why is it complaining about transaction? Am I missing something here?