Skip to content

Commit f0d95e6

Browse files
committed
Fix error handling in validate_instances - see HEA-898
1 parent 817e49f commit f0d95e6

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

pipelines/assets/fixtures.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,22 @@ def validate_instances(
162162
model_errors.append(error)
163163
else:
164164
# Validate foreign key values
165-
if field.related_model.__name__ not in valid_keys:
166-
# If related model is part of the fixture, then it should have already been validated
167-
if field.related_model.__name__ in instances:
168-
raise RuntimeError(
169-
"Related model %s not validated yet but needed for %s"
170-
% (field.related_model.__name__, model_name)
171-
)
172-
else:
173-
# The model is not in the fixture, and hasn't been checked already, so use the primary and
174-
# natural keys for already saved instances. Save the keys as a dict mapping to the
175-
# instance, so that we can resolve natural keys later when validating model.clean()
176-
remote_keys = {}
177-
for related_instance in field.related_model.objects.all():
178-
remote_keys[related_instance.pk] = related_instance
179-
if hasattr(field.related_model, "natural_key"):
180-
remote_keys[related_instance.natural_key()] = related_instance
181-
valid_keys[field.related_model.__name__] = remote_keys
165+
# If related model is part of the fixture, then it should have already been validated
166+
if field.related_model.__name__ in instances and not valid_keys[field.related_model.__name__]:
167+
raise RuntimeError(
168+
"Related model %s not validated yet but needed for %s"
169+
% (field.related_model.__name__, model_name)
170+
)
171+
elif field.related_model.__name__ not in valid_keys:
172+
# The model is not in the fixture, and hasn't been checked already, so use the primary and
173+
# natural keys for already saved instances. Save the keys as a dict mapping to the
174+
# instance, so that we can resolve natural keys later when validating model.clean()
175+
remote_keys = {}
176+
for related_instance in field.related_model.objects.all():
177+
remote_keys[related_instance.pk] = related_instance
178+
if hasattr(field.related_model, "natural_key"):
179+
remote_keys[related_instance.natural_key()] = related_instance
180+
valid_keys[field.related_model.__name__] = remote_keys
182181

183182
if not field.null and not instance[column]:
184183
error = f"Missing mandatory foreign key {column} for {record_reference}"
@@ -271,8 +270,6 @@ def validate_instances(
271270
model_errors.append(f"Error during clean(): {e} for {record_reference}.")
272271
except Exception as e:
273272
model_errors.append(f"Error creating {model_name} instance: {e} for {record_reference}.")
274-
# Ignore errors creating the instance for clean()
275-
pass
276273

277274
if model_errors:
278275
errors += model_errors

0 commit comments

Comments
 (0)