使用 gaerdbms 方言时出现 SQLAlchemy StaleDataError
SQLAlchemy StaleDataError when using gaerdbms dialect
我在 Google Cloud SQL
中使用 sqlalchemy(版本 0.9.9)进行更新时遇到此错误
self.__sql_session__.commit()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 788, in commit
self.transaction.commit()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 384, in commit
self._prepare_impl()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 364, in _prepare_impl
self.session.flush()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 1985, in flush
self._flush(objects)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 2103, in _flush
transaction.rollback(_capture_exception=True)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\util\langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 2067, in _flush
flush_context.execute()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\unitofwork.py", line 372, in execute
rec.execute(self)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\unitofwork.py", line 526, in execute
uow
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\persistence.py", line 60, in save_obj
mapper, table, update)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\persistence.py", line 536, in _emit_update_statements
(table.description, len(update), rows))
StaleDataError: UPDATE statement on table 'tblprocessinstance' expected to update 3 row(s); 0 were matched.
这是我创建引擎的代码。
engine = create_engine('mysql+gaerdbms:///{dbname}?instance={instancename}'.format(dbname=DATABASE_NAME, instancename=config.SQL_INSTANCE), echo=True)
当我尝试在同一事务中两次更新浮点值时会发生此错误。但是我无法直接复制它。重现此错误的一种粗略方法是尝试更新字符串字段中的浮点值 (0.0)。
只有在使用 mysql+gaerdbms 方言 时才会发生这种情况。它与 mysql+googledialect 完美配合。这是我的 SQL 日志。
INFO 2015-04-06 10:36:16,851 sqlsession.py:148] queryFromDataStore metadata Sh20b72f6da1bd1e3f8fa0d16a8c8e63f6 basequer {('Ced010af0ba50bd269b6025611c75a4fa8', '='): 'BBCDC98DC51199A5AC1AE0CD1996ED007'}
INFO 2015-04-06 10:36:16,851 log.py:109] UPDATE tblprocessinstance SET `WholeActual`=%s, `WholeExpectedCmpl`=%s, `WholeCSLA`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,851 log.py:109] (0.014682857186666558, 168.0047622858048, 48.0, 'PI5436634A02BC2D7CF14734C68AF9959F')
INFO 2015-04-06 10:36:16,851 log.py:109] UPDATE tblprocessinstance SET `WholeActual`=%s, `WholeExpectedCmpl`=%s, `WholeCSLA`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,851 log.py:109] (0.014682857186666558, 168.0047622858048, 48.0, 'PI7F2B8C94354BD02C83816B003973AAAD')
INFO 2015-04-06 10:36:16,851 log.py:109] UPDATE tblprocessinstance SET `InstanceActivity`=%s, `BusinessStepName`=%s, `ParticipatedUsers`=%s, `PendingProcess`=%s, `WholeActual`=%s, `WholeExpectedCmpl`=%s, `WholeCSLA`=%s, `ContextActivity`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,851 log.py:109] ('Acc2d12780_3a71_11e4_9b49_008cfaa2a53e', 'Multi Approval,', 'adhi@metalimits.com,adhi@metalimits.com,adhi@metalimits.com,', 'Prc158b60f_dc48_11e4_b6a5_1dbf5e478a01', 0.014682857186666558, 168.0047622858048, 48.0, 'Acc2cce1c0_3a71_11e4_a73f_008cfaa2a53e', 'Pr944d2fc0_dc48_11e4_ba9f_1dbf5e478a01')
INFO 2015-04-06 10:36:16,865 log.py:109] SELECT sysprocessinstance.`Caption` AS `sysprocessinstance_Caption`, sysprocessinstance.`ModifiedBy` AS `sysprocessinstance_ModifiedBy`, sysprocessinstance.`ModifiedAt` AS `sysprocessinstance_ModifiedAt`, sysprocessinstance.`Tenant` AS `sysprocessinstance_Tenant`, sysprocessinstance.`CreatedAt` AS `sysprocessinstance_CreatedAt`, sysprocessinstance.`SheetMetadataName` AS `sysprocessinstance_SheetMetadataName`, sysprocessinstance.`SheetId` AS `sysprocessinstance_SheetId`, sysprocessinstance.`ModifiedIn` AS `sysprocessinstance_ModifiedIn`, sysprocessinstance.`CreatedBy` AS `sysprocessinstance_CreatedBy`, sysprocessinstance.`SheetName` AS `sysprocessinstance_SheetName`, sysprocessinstance.`Metadata` AS `sysprocessinstance_Metadata`, sysprocessinstance.`AssignedTo` AS `sysprocessinstance_AssignedTo`, sysprocessinstance.`Status` AS `sysprocessinstance_Status`, sysprocessinstance.`ProcessInstance` AS `sysprocessinstance_ProcessInstance`, sysprocessinstance.type_ AS sysprocessinstance_type_, tblprocessinstance.type_ AS tblprocessinstance_type_, tblprocessinstance.`CreatedMonth` AS `tblprocessinstance_CreatedMonth`, tblprocessinstance.`Weekends` AS `tblprocessinstance_Weekends`, tblprocessinstance.`ParentProcess` AS `tblprocessinstance_ParentProcess`, tblprocessinstance.`BusinessStep` AS `tblprocessinstance_BusinessStep`, tblprocessinstance.`StartStep` AS `tblprocessinstance_StartStep`, tblprocessinstance.`CreatedDay` AS `tblprocessinstance_CreatedDay`, tblprocessinstance.`ExpectedAt` AS `tblprocessinstance_ExpectedAt`, tblprocessinstance.`SLA_Time` AS `tblprocessinstance_SLA_Time`, tblprocessinstance.`InstanceId` AS `tblprocessinstance_InstanceId`, tblprocessinstance.`InstanceActivity` AS `tblprocessinstance_InstanceActivity`, tblprocessinstance.`InstanceMetadata` AS `tblprocessinstance_InstanceMetadata`, tblprocessinstance.`Rejected` AS `tblprocessinstance_Rejected`, tblprocessinstance.`ModifiedWeekDay` AS `tblprocessinstance_ModifiedWeekDay`, tblprocessinstance.`InstanceAssignedTo` AS `tblprocessinstance_InstanceAssignedTo`, tblprocessinstance.`Stage` AS `tblprocessinstance_Stage`, tblprocessinstance.`ActivityName` AS `tblprocessinstance_ActivityName`, tblprocessinstance.`InstanceAssignedFor` AS `tblprocessinstance_InstanceAssignedFor`, tblprocessinstance.`NextTriggerTime` AS `tblprocessinstance_NextTriggerTime`, tblprocessinstance.`IsDeleted` AS `tblprocessinstance_IsDeleted`, tblprocessinstance.`QueryCount` AS `tblprocessinstance_QueryCount`, tblprocessinstance.`BusinessStepName` AS `tblprocessinstance_BusinessStepName`, tblprocessinstance.`PreviousPending` AS `tblprocessinstance_PreviousPending`, tblprocessinstance.`OnBehalfOf` AS `tblprocessinstance_OnBehalfOf`, tblprocessinstance.`ExpectedCmpl` AS `tblprocessinstance_ExpectedCmpl`, tblprocessinstance.`ParticipatedUsers` AS `tblprocessinstance_ParticipatedUsers`, tblprocessinstance.`PendingProcess` AS `tblprocessinstance_PendingProcess`, tblprocessinstance.`SLA` AS `tblprocessinstance_SLA`, tblprocessinstance.`Actual` AS `tblprocessinstance_Actual`, tblprocessinstance.`CSLA` AS `tblprocessinstance_CSLA`, tblprocessinstance.`NodeType` AS `tblprocessinstance_NodeType`, tblprocessinstance.`Active` AS `tblprocessinstance_Active`, tblprocessinstance.`Counter` AS `tblprocessinstance_Counter`, tblprocessinstance.`ExpectedWeekDay` AS `tblprocessinstance_ExpectedWeekDay`, tblprocessinstance.`Comment` AS `tblprocessinstance_Comment`, tblprocessinstance.`Iterations` AS `tblprocessinstance_Iterations`, tblprocessinstance.`WholeActual` AS `tblprocessinstance_WholeActual`, tblprocessinstance.`FollowingUsers` AS `tblprocessinstance_FollowingUsers`, tblprocessinstance.`WholeExpectedCmpl` AS `tblprocessinstance_WholeExpectedCmpl`, tblprocessinstance.`WholeCSLA` AS `tblprocessinstance_WholeCSLA`, tblprocessinstance.`NoOfParticipants` AS `tblprocessinstance_NoOfParticipants`, tblprocessinstance.`WholeSLA` AS `tblprocessinstance_WholeSLA`, tblprocessinstance.`ContextActivity` AS `tblprocessinstance_ContextActivity`, tblprocessinstance.`ContextBranch` AS `tblprocessinstance_ContextBranch`, tempprocessinstance.id AS tempprocessinstance_id, sysprocessinstance.id AS sysprocessinstance_id, tblprocessinstance.id AS tblprocessinstance_id
FROM tblprocessinstance INNER JOIN sysprocessinstance ON tblprocessinstance.id = sysprocessinstance.id INNER JOIN tempprocessinstance ON sysprocessinstance.id = tempprocessinstance.id
WHERE tblprocessinstance.`StartStep` = %s
LIMIT %s
INFO 2015-04-06 10:36:16,928 log.py:109] UPDATE tblprocessinstance SET `WholeExpectedCmpl`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,928 log.py:109] (168.00476228580476, 'PI5436634A02BC2D7CF14734C68AF9959F')
INFO 2015-04-06 10:36:16,944 log.py:109] UPDATE tblprocessinstance SET `WholeExpectedCmpl`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,944 log.py:109] (168.00476228580476, 'PI7F2B8C94354BD02C83816B003973AAAD')
INFO 2015-04-06 10:36:16,944 log.py:109] UPDATE tblprocessinstance SET `ExpectedCmpl`=%s, `WholeExpectedCmpl`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,944 log.py:109] (168.00476228580476, 168.00476228580476, 'Pr944d2fc0_dc48_11e4_ba9f_1dbf5e478a01')
任何人都可以建议我一个解决方案吗?是 SA 中的错误吗?
谢谢
阿迪
mysql+gaerdbms 使用的是与常规不完全兼容的旧连接。从 SQL Alchemy 连接的推荐方法是使用如下连接字符串:mysql+mysqldb://root@/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>
.
参考:https://bitbucket.org/zzzeek/sqlalchemy/issue/3275/gaerdbmspy-is-using-a-deprecated-api-to
我在 Google Cloud SQL
中使用 sqlalchemy(版本 0.9.9)进行更新时遇到此错误
self.__sql_session__.commit()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 788, in commit
self.transaction.commit()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 384, in commit
self._prepare_impl()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 364, in _prepare_impl
self.session.flush()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 1985, in flush
self._flush(objects)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 2103, in _flush
transaction.rollback(_capture_exception=True)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\util\langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\session.py", line 2067, in _flush
flush_context.execute()
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\unitofwork.py", line 372, in execute
rec.execute(self)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\unitofwork.py", line 526, in execute
uow
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\persistence.py", line 60, in save_obj
mapper, table, update)
File "D:\workspace\kissflow\build\runtime\sqlalchemy\orm\persistence.py", line 536, in _emit_update_statements
(table.description, len(update), rows))
StaleDataError: UPDATE statement on table 'tblprocessinstance' expected to update 3 row(s); 0 were matched.
这是我创建引擎的代码。
engine = create_engine('mysql+gaerdbms:///{dbname}?instance={instancename}'.format(dbname=DATABASE_NAME, instancename=config.SQL_INSTANCE), echo=True)
当我尝试在同一事务中两次更新浮点值时会发生此错误。但是我无法直接复制它。重现此错误的一种粗略方法是尝试更新字符串字段中的浮点值 (0.0)。
只有在使用 mysql+gaerdbms 方言 时才会发生这种情况。它与 mysql+googledialect 完美配合。这是我的 SQL 日志。
INFO 2015-04-06 10:36:16,851 sqlsession.py:148] queryFromDataStore metadata Sh20b72f6da1bd1e3f8fa0d16a8c8e63f6 basequer {('Ced010af0ba50bd269b6025611c75a4fa8', '='): 'BBCDC98DC51199A5AC1AE0CD1996ED007'}
INFO 2015-04-06 10:36:16,851 log.py:109] UPDATE tblprocessinstance SET `WholeActual`=%s, `WholeExpectedCmpl`=%s, `WholeCSLA`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,851 log.py:109] (0.014682857186666558, 168.0047622858048, 48.0, 'PI5436634A02BC2D7CF14734C68AF9959F')
INFO 2015-04-06 10:36:16,851 log.py:109] UPDATE tblprocessinstance SET `WholeActual`=%s, `WholeExpectedCmpl`=%s, `WholeCSLA`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,851 log.py:109] (0.014682857186666558, 168.0047622858048, 48.0, 'PI7F2B8C94354BD02C83816B003973AAAD')
INFO 2015-04-06 10:36:16,851 log.py:109] UPDATE tblprocessinstance SET `InstanceActivity`=%s, `BusinessStepName`=%s, `ParticipatedUsers`=%s, `PendingProcess`=%s, `WholeActual`=%s, `WholeExpectedCmpl`=%s, `WholeCSLA`=%s, `ContextActivity`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,851 log.py:109] ('Acc2d12780_3a71_11e4_9b49_008cfaa2a53e', 'Multi Approval,', 'adhi@metalimits.com,adhi@metalimits.com,adhi@metalimits.com,', 'Prc158b60f_dc48_11e4_b6a5_1dbf5e478a01', 0.014682857186666558, 168.0047622858048, 48.0, 'Acc2cce1c0_3a71_11e4_a73f_008cfaa2a53e', 'Pr944d2fc0_dc48_11e4_ba9f_1dbf5e478a01')
INFO 2015-04-06 10:36:16,865 log.py:109] SELECT sysprocessinstance.`Caption` AS `sysprocessinstance_Caption`, sysprocessinstance.`ModifiedBy` AS `sysprocessinstance_ModifiedBy`, sysprocessinstance.`ModifiedAt` AS `sysprocessinstance_ModifiedAt`, sysprocessinstance.`Tenant` AS `sysprocessinstance_Tenant`, sysprocessinstance.`CreatedAt` AS `sysprocessinstance_CreatedAt`, sysprocessinstance.`SheetMetadataName` AS `sysprocessinstance_SheetMetadataName`, sysprocessinstance.`SheetId` AS `sysprocessinstance_SheetId`, sysprocessinstance.`ModifiedIn` AS `sysprocessinstance_ModifiedIn`, sysprocessinstance.`CreatedBy` AS `sysprocessinstance_CreatedBy`, sysprocessinstance.`SheetName` AS `sysprocessinstance_SheetName`, sysprocessinstance.`Metadata` AS `sysprocessinstance_Metadata`, sysprocessinstance.`AssignedTo` AS `sysprocessinstance_AssignedTo`, sysprocessinstance.`Status` AS `sysprocessinstance_Status`, sysprocessinstance.`ProcessInstance` AS `sysprocessinstance_ProcessInstance`, sysprocessinstance.type_ AS sysprocessinstance_type_, tblprocessinstance.type_ AS tblprocessinstance_type_, tblprocessinstance.`CreatedMonth` AS `tblprocessinstance_CreatedMonth`, tblprocessinstance.`Weekends` AS `tblprocessinstance_Weekends`, tblprocessinstance.`ParentProcess` AS `tblprocessinstance_ParentProcess`, tblprocessinstance.`BusinessStep` AS `tblprocessinstance_BusinessStep`, tblprocessinstance.`StartStep` AS `tblprocessinstance_StartStep`, tblprocessinstance.`CreatedDay` AS `tblprocessinstance_CreatedDay`, tblprocessinstance.`ExpectedAt` AS `tblprocessinstance_ExpectedAt`, tblprocessinstance.`SLA_Time` AS `tblprocessinstance_SLA_Time`, tblprocessinstance.`InstanceId` AS `tblprocessinstance_InstanceId`, tblprocessinstance.`InstanceActivity` AS `tblprocessinstance_InstanceActivity`, tblprocessinstance.`InstanceMetadata` AS `tblprocessinstance_InstanceMetadata`, tblprocessinstance.`Rejected` AS `tblprocessinstance_Rejected`, tblprocessinstance.`ModifiedWeekDay` AS `tblprocessinstance_ModifiedWeekDay`, tblprocessinstance.`InstanceAssignedTo` AS `tblprocessinstance_InstanceAssignedTo`, tblprocessinstance.`Stage` AS `tblprocessinstance_Stage`, tblprocessinstance.`ActivityName` AS `tblprocessinstance_ActivityName`, tblprocessinstance.`InstanceAssignedFor` AS `tblprocessinstance_InstanceAssignedFor`, tblprocessinstance.`NextTriggerTime` AS `tblprocessinstance_NextTriggerTime`, tblprocessinstance.`IsDeleted` AS `tblprocessinstance_IsDeleted`, tblprocessinstance.`QueryCount` AS `tblprocessinstance_QueryCount`, tblprocessinstance.`BusinessStepName` AS `tblprocessinstance_BusinessStepName`, tblprocessinstance.`PreviousPending` AS `tblprocessinstance_PreviousPending`, tblprocessinstance.`OnBehalfOf` AS `tblprocessinstance_OnBehalfOf`, tblprocessinstance.`ExpectedCmpl` AS `tblprocessinstance_ExpectedCmpl`, tblprocessinstance.`ParticipatedUsers` AS `tblprocessinstance_ParticipatedUsers`, tblprocessinstance.`PendingProcess` AS `tblprocessinstance_PendingProcess`, tblprocessinstance.`SLA` AS `tblprocessinstance_SLA`, tblprocessinstance.`Actual` AS `tblprocessinstance_Actual`, tblprocessinstance.`CSLA` AS `tblprocessinstance_CSLA`, tblprocessinstance.`NodeType` AS `tblprocessinstance_NodeType`, tblprocessinstance.`Active` AS `tblprocessinstance_Active`, tblprocessinstance.`Counter` AS `tblprocessinstance_Counter`, tblprocessinstance.`ExpectedWeekDay` AS `tblprocessinstance_ExpectedWeekDay`, tblprocessinstance.`Comment` AS `tblprocessinstance_Comment`, tblprocessinstance.`Iterations` AS `tblprocessinstance_Iterations`, tblprocessinstance.`WholeActual` AS `tblprocessinstance_WholeActual`, tblprocessinstance.`FollowingUsers` AS `tblprocessinstance_FollowingUsers`, tblprocessinstance.`WholeExpectedCmpl` AS `tblprocessinstance_WholeExpectedCmpl`, tblprocessinstance.`WholeCSLA` AS `tblprocessinstance_WholeCSLA`, tblprocessinstance.`NoOfParticipants` AS `tblprocessinstance_NoOfParticipants`, tblprocessinstance.`WholeSLA` AS `tblprocessinstance_WholeSLA`, tblprocessinstance.`ContextActivity` AS `tblprocessinstance_ContextActivity`, tblprocessinstance.`ContextBranch` AS `tblprocessinstance_ContextBranch`, tempprocessinstance.id AS tempprocessinstance_id, sysprocessinstance.id AS sysprocessinstance_id, tblprocessinstance.id AS tblprocessinstance_id
FROM tblprocessinstance INNER JOIN sysprocessinstance ON tblprocessinstance.id = sysprocessinstance.id INNER JOIN tempprocessinstance ON sysprocessinstance.id = tempprocessinstance.id
WHERE tblprocessinstance.`StartStep` = %s
LIMIT %s
INFO 2015-04-06 10:36:16,928 log.py:109] UPDATE tblprocessinstance SET `WholeExpectedCmpl`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,928 log.py:109] (168.00476228580476, 'PI5436634A02BC2D7CF14734C68AF9959F')
INFO 2015-04-06 10:36:16,944 log.py:109] UPDATE tblprocessinstance SET `WholeExpectedCmpl`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,944 log.py:109] (168.00476228580476, 'PI7F2B8C94354BD02C83816B003973AAAD')
INFO 2015-04-06 10:36:16,944 log.py:109] UPDATE tblprocessinstance SET `ExpectedCmpl`=%s, `WholeExpectedCmpl`=%s WHERE tblprocessinstance.id = %s
INFO 2015-04-06 10:36:16,944 log.py:109] (168.00476228580476, 168.00476228580476, 'Pr944d2fc0_dc48_11e4_ba9f_1dbf5e478a01')
任何人都可以建议我一个解决方案吗?是 SA 中的错误吗?
谢谢
阿迪
mysql+gaerdbms 使用的是与常规不完全兼容的旧连接。从 SQL Alchemy 连接的推荐方法是使用如下连接字符串:mysql+mysqldb://root@/<dbname>?unix_socket=/cloudsql/<projectid>:<instancename>
.
参考:https://bitbucket.org/zzzeek/sqlalchemy/issue/3275/gaerdbmspy-is-using-a-deprecated-api-to