|
5 | 5 | from django.apps import apps |
6 | 6 | from django.db import models as django_models |
7 | 7 | from django.db.models.options import Options |
| 8 | +from django.db.models import Max |
8 | 9 | from django.utils.translation import gettext_lazy as _ |
9 | 10 |
|
10 | 11 | from ..remote import RemoteObject, get_local_resource_prefix, get_resource_prefix |
@@ -176,12 +177,17 @@ def get_for_id(self, id: int) -> django_models.Model: |
176 | 177 |
|
177 | 178 | def load_remote_objects(self, remote_data: list[dict]): |
178 | 179 | parent_mapping: dict[django_models.Model, str] = {} |
| 180 | + # For test reasons, it can be very hard to assure the post_migrate logic runs in all cases |
| 181 | + # so we just put in the id field manually which avoids any conflict with existing records |
| 182 | + max_id = self.aggregate(Max('id'))['id__max'] or 0 |
179 | 183 | for remote_type_raw in remote_data: |
180 | | - remote_type = remote_type_raw.copy() |
181 | | - service = remote_type.pop('service') |
182 | | - model = remote_type.pop('model') |
183 | | - pct_slug = remote_type.pop('parent_content_type') |
184 | | - ct, _ = self.get_or_create(service=service, model=model, defaults=remote_type) |
| 184 | + defaults = remote_type_raw.copy() |
| 185 | + service = defaults.pop('service') |
| 186 | + model = defaults.pop('model') |
| 187 | + pct_slug = defaults.pop('parent_content_type') |
| 188 | + max_id += 1 |
| 189 | + defaults['id'] = max_id |
| 190 | + ct, _ = self.get_or_create(service=service, model=model, defaults=defaults) |
185 | 191 | parent_mapping[ct] = pct_slug |
186 | 192 |
|
187 | 193 | # The parent type link needs to be filled in via a second pass |
|
0 commit comments