Commits

Chad Dombrova  committed e4d15b8

Update SqlAlchemyModelInspector.get_objects_affected_by() to also accept a batch of primary keys,
like its Django counterpart.

  • Participants
  • Parent commits d7746d7
  • Branches external_triggers

Comments (0)

Files changed (1)

File denormalize/orms/sqlalchemy.py

         return self._session.query(self._model).all()
 
     # FIXME: move to CollectionModel?
-    def get_objects_affected_by(self, filter_paths, model, primary_key):
+    def get_objects_affected_by(self, filter_paths, model, primary_keys):
         if not filter_paths:
             return set([])
 
+        if isinstance(primary_keys, (list, set)):
+            batched = True
+        else:
+            if not isinstance(primary_keys, tuple):
+                raise TypeError("primary_key must be a tuple")
+            batched = False
+
         # FIXME: look into using the session from the instance:
         # session = sqlalchemy.inspect(inspect).session
 
         select = []
-        for key in self.primary_keys():
-            select.append(getattr(self._model, key.accessor))
-
-        filt = {}
-        for key, pk in zip(model.primary_keys(), primary_key):
-            filt[key.accessor] = pk
+        filt = []
+        for i, key in enumerate(self.primary_keys()):
+            prop = getattr(self._model, key.accessor)
+            select.append(prop)
+            if batched:
+                # primary_keys is a list of tuples
+                filt.append(prop.in_([x[i] for x in primary_keys]))
+            else:
+                # primary_keys is a tuple
+                filt.append(prop == primary_keys[i])
 
         queries = []
         for filter_path in filter_paths:
             join_paths = [x.accessor for x in filter_path]
             q = self._session.query(*select).join(*join_paths)
-            queries.append(q.filter_by(**filt))
+            queries.append(q.filter(*filt))
         result = self._session.query(distinct(*select)).union(*queries).all()
 
         return set(result)