Sqlite3 Unique Constraint Failed Error

SQLite3 UNIQUE constraint failed error

You have set list_id to be the primary key on the list table, which means that value must be unique for each record. Trying to insert multiple records with the same list_id table is therefore causing the error.

The issue is the same for the item table.

SQLite IntegrityError: UNIQUE constraint failed:

The UNIQUE clause of your table's declaration states that every row's combination of player_id, map_id, and runID must be unique. You are getting this error because there is already a row in the jumptimes table withplayer_id = 13, map_id = 34, and runID = 0.

Here is a simple SQLFiddle reproducing the error. The "constraint" the error is talking about is the UNIQUE clause, and if you could see the full error, it would be the same as the one you're getting.

UNIQUE constraint failed: sqlite database : android

Your code probably violates primary key's uniqueness constraint on a KEY_ID field.

Two possible solutions are:

  1. Make sure that your EventData.getId() returns unique values per object. For now, I don't see you pass any identifier to its constructor and perhaps all the events are inserted with the same id value.
  2. If you don't care about generating ids by yourself, you can add AUTOINCREMENT setting to your KEY_ID column definition. This way KEY_ID field will be filled automatically and each row will have its own, unique value. Once there, don't forget to remove adding KEY_ID to ContentValues by yourself.

sqlite3.IntegrityError: UNIQUE constraint failed | When using bulk adding many-to-many relation

You should probably check if the reason why you are having those errors is because of duplicate data. If that's the case, I think the ignore_conflicts parameter of bulk_create can solve your problem. It will save every valid entry and ignore those who have duplicate conflict.

Check the doc:
https://docs.djangoproject.com/en/3.2/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create



Related Topics



Leave a reply



Submit