Cédric Krier pushed to branch branch/default at Tryton / Tryton


Commits:
a99b5334 by Cédric Krier at 2023-04-22T18:29:44+02:00
Do not update related records by foreign key if they are going to be deleted

Closes #3689
- - - - -
5b67535b by Cédric Krier at 2023-04-22T18:31:30+02:00
Do not override deleted record instances by related records
- - - - -
60dfdf34 by Cédric Krier at 2023-04-22T18:43:45+02:00
Delete on cascade locations

Closes #3689
- - - - -


2 changed files:

- modules/stock/location.py
- trytond/trytond/model/modelsql.py


Changes:

=====================================
modules/stock/location.py
=====================================
@@ -59,7 +59,7 @@
         ], "Location type")
     type_string = type.translated('type')
     parent = fields.Many2One(
-        "stock.location", "Parent",
+        "stock.location", "Parent", ondelete='CASCADE',
         left="left", right="right",
         help="Used to add structure above the location.")
     left = fields.Integer('Left', required=True)
@@ -573,8 +573,16 @@
                             warehouse=warehouse.rec_name))
 
     @classmethod
-    def delete(cls, *args):
-        super().delete(*args)
+    def delete(cls, locations):
+        # Delete also required children as CASCADING is done separately
+        extra_locations = []
+        for location in locations:
+            extra_locations.extend(filter(None, [
+                        location.input_location,
+                        location.output_location,
+                        location.storage_location,
+                        ]))
+        super().delete(locations + extra_locations)
         cls._default_warehouse_cache.clear()
 
     @classmethod


=====================================
trytond/trytond/model/modelsql.py
=====================================
@@ -1431,6 +1431,6 @@
                     Column(foreign_table, field_name), sub_ids)
                 cursor.execute(*foreign_table.select(foreign_table.id,
                         where=foreign_red_sql))
-                records = Model.browse([x[0] for x in cursor])
+                related_records = Model.browse([x[0] for x in cursor])
             else:
                 with inactive_records():
@@ -1435,7 +1435,10 @@
             else:
                 with inactive_records():
-                    records = Model.search([(field_name, 'in', sub_ids)])
-            return records
+                    related_records = Model.search(
+                        [(field_name, 'in', sub_ids)])
+            if Model == cls:
+                related_records = list(set(related_records) - set(records))
+            return related_records
 
         for sub_ids, sub_records in zip(
                 grouped_slice(ids), grouped_slice(records)):
@@ -1446,9 +1449,10 @@
                 if (not hasattr(Model, 'search')
                         or not hasattr(Model, 'write')):
                     continue
-                records = get_related_records(Model, field_name, sub_ids)
-                if records:
-                    Model.write(records, {
+                related_records = get_related_records(
+                    Model, field_name, sub_ids)
+                if related_records:
+                    Model.write(related_records, {
                             field_name: None,
                             })
 
@@ -1456,9 +1460,10 @@
                 if (not hasattr(Model, 'search')
                         or not hasattr(Model, 'delete')):
                     continue
-                records = get_related_records(Model, field_name, sub_ids)
-                if records:
-                    Model.delete(records)
+                related_records = get_related_records(
+                    Model, field_name, sub_ids)
+                if related_records:
+                    Model.delete(related_records)
 
             for Model, field_name in foreign_keys_tocheck:
                 with without_check_access(), inactive_records():



View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/compare/0a676bccac7931342cbb2c22c8fa4555fd4c2614...60dfdf344edc69e546ab24121a40cbdce4fabb41

-- 
View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/compare/0a676bccac7931342cbb2c22c8fa4555fd4c2614...60dfdf344edc69e546ab24121a40cbdce4fabb41
You're receiving this email because of your account on foss.heptapod.net.


Reply via email to