Browse Source

Resolve SAWarning in Query.soft_delete()

We currently see a lot of warnings like this from
the soft_delete() method:

  sqlalchemy.exc.SAWarning: Evaluating non-mapped column expression
  'updated_at' onto ORM instances; this is a deprecated use case.
  Please make use of the actual mapped columns in ORM-evaluated
  UPDATE / DELETE expressions.

This is because the "evaluate" synchronization strategy would like
to search for objects and update them based on the UPDATE criteria
passed, however the columns given, literal_column('id'),
literal_column('updated_at'), are not mapped to anything. The
evaluator has to make a guess that the string contained in these
expressions should be matched to a mapped attribute on the given
entity and this guess was first removed in [1], then added back in
[2] with a warning (likely since oslo.db is invoking it).

This uses the actual entity-mapped column for the query rather
than the literal string column.

[1] https://docs.sqlalchemy.org/en/latest/changelog/changelog_12.html#change-b1e620dece
[2] https://docs.sqlalchemy.org/en/latest/changelog/changelog_12.html#change-dff3a46978

Change-Id: I192e84ce757d12d33085a209dd58d8ea46fb90fb
Closes-Bug: #1814199
Mike Bayer 2 months ago
parent
commit
af4b2263e4
1 changed files with 3 additions and 3 deletions
  1. 3
    3
      oslo_db/sqlalchemy/orm.py

+ 3
- 3
oslo_db/sqlalchemy/orm.py View File

@@ -18,7 +18,6 @@
18 18
 
19 19
 from oslo_utils import timeutils
20 20
 import sqlalchemy.orm
21
-from sqlalchemy.sql.expression import literal_column
22 21
 
23 22
 from oslo_db.sqlalchemy import update_match
24 23
 
@@ -26,8 +25,9 @@ from oslo_db.sqlalchemy import update_match
26 25
 class Query(sqlalchemy.orm.query.Query):
27 26
     """Subclass of sqlalchemy.query with soft_delete() method."""
28 27
     def soft_delete(self, synchronize_session='evaluate'):
29
-        return self.update({'deleted': literal_column('id'),
30
-                            'updated_at': literal_column('updated_at'),
28
+        entity = self.column_descriptions[0]['entity']
29
+        return self.update({'deleted': entity.id,
30
+                            'updated_at': entity.updated_at,
31 31
                             'deleted_at': timeutils.utcnow()},
32 32
                            synchronize_session=synchronize_session)
33 33
 

Loading…
Cancel
Save