1111from django .db import connections
1212from django .db import router
1313from django .db import transaction
14+ from django .db .models import CharField
1415from django .db .models import Q
1516from django .db .models import signals
1617from django .utils import timezone
@@ -278,7 +279,7 @@ def _validate_missing_store_foreign_keys(from_model_name, to_model_name, temp_ta
278279 """
279280 invalid_pks = []
280281 select_sql = """
281- SELECT t.from_pk, t.to_pk
282+ SELECT t.from_field, t. from_pk, t.to_pk
282283 FROM {temp_table} t
283284 WHERE NOT EXISTS (
284285 SELECT 1
@@ -297,18 +298,27 @@ def _validate_missing_store_foreign_keys(from_model_name, to_model_name, temp_ta
297298 store_update_fields = [Store ._meta .pk , store_deserialization_error ]
298299
299300 from_pk_field = temp_table .get_field ("from_pk" )
301+ to_pk_field = temp_table .get_field ("to_pk" )
300302 update_values = []
301303 with connection .cursor () as c :
302304 c .execute (select_sql )
303- for from_pk , to_pk in c .fetchall ():
304- err = "Error deserializing instance of {from_model} with id {from_pk}: missing {to_model} with id {to_pk}" .format (
305- from_model = from_model_name ,
306- from_pk = from_pk ,
307- to_model = to_model_name ,
308- to_pk = to_pk ,
305+ for from_field , from_pk , to_pk in c .fetchall ():
306+ err = dict (
307+ {
308+ from_field : "{to_model_name} instance with id '{to_pk}' does not exist" .format (
309+ to_model_name = to_model_name ,
310+ to_pk = to_pk_field .to_python (to_pk ),
311+ )
312+ }
313+ )
314+ logger .warning (
315+ "Error deserializing instance of {from_model} with id {from_pk}: {err}" .format (
316+ from_model = from_model_name ,
317+ from_pk = from_pk ,
318+ err = str (err ),
319+ )
309320 )
310- logger .warning (err )
311- update_values .extend ([from_pk , err ])
321+ update_values .extend ([from_pk , str (err )])
312322 invalid_pks .append (from_pk_field .to_python (from_pk ))
313323 if update_values :
314324 # update Store with errors
@@ -384,7 +394,11 @@ def _validate_store_foreign_keys(from_model_name, fk_references):
384394
385395 for to_model_name , to_fk_references in fk_references .items ():
386396 with TemporaryTable (
387- connection , "fks" , from_pk = UUIDField (), to_pk = UUIDField ()
397+ connection ,
398+ "fks" ,
399+ from_field = CharField (max_length = 255 ),
400+ from_pk = UUIDField (),
401+ to_pk = UUIDField (),
388402 ) as temp_table :
389403 # insert all the FK references into a temp table in the database
390404 temp_table .bulk_insert ([fks ._asdict () for fks in to_fk_references ])
@@ -517,7 +531,11 @@ def _deserialize_from_store(profile, skip_erroring=False, filter=None):
517531 # deserialize for reasons other than broken FKs at this point
518532 for fk_ref in fk_refs :
519533 if fk_ref .to_pk in excluded_list :
520- raise exceptions .ValidationError ("{} with id {} failed to deserialize" .format (fk_model , fk_ref .to_pk ))
534+ raise exceptions .ValidationError (
535+ "{} with id {} failed to deserialize" .format (
536+ fk_model , fk_ref .to_pk
537+ )
538+ )
521539 deferred_fks [fk_model ].extend (fk_refs )
522540 except (
523541 exceptions .ValidationError ,
0 commit comments