Project

General

Profile

Bug #97672

dataviz, IntegrityError: duplicate key value violates unique constraint "dataviz_chartfilterscell_pkey"

Added by Sentry Io about 1 month ago. Updated about 1 month ago.

Status:
Nouveau
Priority:
Normal
Target version:
-
Start date:
28 October 2024
Due date:
% Done:

0%

Estimated time:
Patch proposed:
No
Planning:
No

Description

https://sentry.entrouvert.org/entrouvert/publik/issues/134543/

UniqueViolation: duplicate key value violates unique constraint "dataviz_chartfilterscell_pkey" 
DETAIL:  Key (id)=(104) already exists.

  File "django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

IntegrityError: duplicate key value violates unique constraint "dataviz_chartfilterscell_pkey" 
DETAIL:  Key (id)=(104) already exists.

(49 additional frame(s) were not displayed)
...
  File "django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "django/db/backends/utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

History

#1

Updated by Valentin Deniaud about 1 month ago

  • Project changed from Suivi des traces to Combo
  • Subject changed from IntegrityError: duplicate key value violates unique constraint "dataviz_chartfilterscell_pkey" to dataviz, IntegrityError: duplicate key value violates unique constraint "dataviz_chartfilterscell_pkey"
  • Assignee set to Valentin Deniaud
#2

Updated by Valentin Deniaud about 1 month ago

Je ne sais pas ce qu'il s'est passé pour avoir cette trace, ça m'a l'air d'être django qui se plante en essayant de créer l'objet au lieu de le mettre à jour.

https://docs.djangoproject.com/en/dev/ref/models/instances/#how-django-knows-to-update-vs-insert dit :

You may have noticed Django database objects use the same save() method for creating and changing objects. Django abstracts the need to use INSERT or UPDATE SQL statements. Specifically, when you call save() and the object’s primary key attribute does not define a default or db_default, Django follows this algorithm:

If the object’s primary key attribute is set to a value that evaluates to True (i.e., a value other than None or the empty string), Django executes an UPDATE.
If the object’s primary key attribute is not set or if the UPDATE didn’t update anything (e.g. if primary key is set to a value that doesn’t exist in the database), Django executes an INSERT.

Dans sentry on voit bien ces deux requêtes être faites :

UPDATE "dataviz_chartfilterscell" SET "uuid" = %s, "page_id" = %s, "placeholder" = %s, "order" = %s, "slug" = %s, "extra_css_class" = %s, "template_name" = NULL, "condition" = %s, "public" = %s, "restricted_to_unlogged" = %s, "last_update_timestamp" = %s, "filters" = %s WHERE "dataviz_chartfilterscell"."id" = %s
INSERT INTO "dataviz_chartfilterscell" ("id", "uuid", "page_id", "placeholder", "order", "slug", "extra_css_class", "template_name", "condition", "public", "restricted_to_unlogged", "last_update_timestamp", "filters") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING "dataviz_chartfilterscell"."id" 

La doc Django dit aussi :

There are some rare cases where the database doesn’t report that a row was updated even if the database contains a row for the object’s primary key value. An example is the PostgreSQL ON UPDATE trigger which returns NULL.

Je ne sais pas si on peut se trouver dans un de ces rares cas.

Un patch sans comprendre qui fonctionnerait pour ne plus avoir cette trace ce serait ve simplement passer « force_update=True » à ce save().

Also available in: Atom PDF