From 098f1596fa82d4f2a509f549e50f13e07d0c5ded Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Tue, 6 Feb 2024 13:14:01 +0100 Subject: ✨ scripts: ishtar_operation_fix_merged_towns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/models_common.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'ishtar_common/models_common.py') diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index e4438be43..928b22630 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -2946,12 +2946,17 @@ class GeographicItem(models.Model): self.geodata.add(self.main_geodata) except (OperationalError, IntegrityError): pass - elif not self.main_geodata and self.geodata.count(): - # arbitrary associate the first to geodata - self.main_geodata = self.geodata.order_by("pk").all()[0] - self.skip_history_when_saving = True - self._no_move = True - self.save() + elif not self.main_geodata: + try: + with transaction.atomic(): + if self.geodata.count(): + # arbitrary associate the first to geodata + self.main_geodata = self.geodata.order_by("pk").all()[0] + self.skip_history_when_saving = True + self._no_move = True + self.save() + except (OperationalError, IntegrityError, IndexError): + pass @property def geodata_list(self): -- cgit v1.2.3