sqlite3.IntegrityError: UNIQUE constraint failed (Python)

47 Views Asked by At

I am using Python and Sqlite 3 for a program

I have created following two tables in my database

theCursor.execute("""CREATE TABLE IF NOT EXISTS
    taxpayers(txp_id TEXT PRIMARY KEY, name TEXT, type TEXT)""")

theCursor.execute("""CREATE TABLE IF NOT EXISTS
    inventory(txp_id TEXT, item TEXT, hscode TEXT, rate TEXT, qty TEXT, stock TEXT, UNIQUE(txp_id, hscode)
""")

I want to add a unique combination for txp_id and hscode in inventory table. When I run my program and try to add record into inventory, I get this error:

program v6.py", line 217, in addToItemDB
    theCursor.execute("INSERT INTO inventory VALUES(:txp_id, :item, :hscode, :rate, :qty, :stock)",
sqlite3.IntegrityError: UNIQUE constraint failed: inventory.txp_id

Please help

The txp_id is unique for first table

In second table I want to have many hscodes for every txp_id so kept the combination unique so that evey row will be unique even if each of them is not unique in this table.

I add the first record successfully but when I add another record I get this error even if I enter different hscode for same txp_id

0

There are 0 best solutions below