I’m baffled by this error. I have some code that essentially does the following:
try:
with transaction.atomic():
name = "Control diet"
category = "animal_treatment"
protocol_rec, protocol_created = Protocol.objects.using('validation').get_or_create(name=name, category=category)
except IntegrityError as e:
print(f"Existing records: {', '.join([str(model_to_dict(r)) for r in Protocol.objects.using("validation").all()])}")
traceback.print_exc()
print(f"{type(e).__name__} in the {db} database on data row {index + 1}, creating {category} record for protocol '{name}' with description '{description}': {e}")
This is just a snippet with some hard-coded values. This is actually in a loop (above the try block).
And I’m seeing this:
Existing records: {'id': 1, 'name': 'Default', 'description': "For protocol's full text, please consult Michael Neinast.", 'category': 'msrun_protocol'}
Traceback (most recent call last):
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 581, in get_or_create
return self.get(**kwargs), False
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 435, in get
raise self.model.DoesNotExist(
DataRepo.models.protocol.Protocol.DoesNotExist: Protocol matching query does not exist.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "DataRepo_protocol_pkey"
DETAIL: Key (id)=(1) already exists.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/DataRepo/utils/protocols_loader.py", line 89, in load_database
protocol_rec, protocol_created = Protocol.objects.using(
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 588, in get_or_create
return self.create(**params), True
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 453, in create
obj.save(force_insert=True, using=self.db)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/base.py", line 763, in save_base
updated = self._save_table(
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/base.py", line 868, in _save_table
results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/base.py", line 906, in _do_insert
return manager._insert(
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/query.py", line 1270, in _insert
return query.get_compiler(using=using).execute_sql(returning_fields)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql
cursor.execute(sql, params)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/rleach/PROJECT-LOCAL/TRACEBASE/tracebase/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "DataRepo_protocol_pkey"
DETAIL: Key (id)=(1) already exists.
IntegrityError in the validation database on data row 1, creating animal_treatment record for protocol 'Control diet' with description 'Mice on control AA purified diet for 3 weeks': duplicate key value violates unique constraint "DataRepo_protocol_pkey"
DETAIL: Key (id)=(1) already exists.
The load file only has 2 protocols it’s loading (the above one being the first) and the second one loads fine. The database is pre-loaded with 1 “msrun_protocol” (the one in the print) before this script runs. So it seems like the get_or_create
correctly does not find the protocol record, but tries to create it with the AutoField value of 1, but 1 already exists in a record! This code was previously working. Changes in the current effort never even touched this script and I’m running out of leads as to the cause of the problem.