'Sqllite - IntegrityError: UNIQUE constraint failed:

I am wondering why in the following example my Sqllite3 in Python command does not work:

I am creating the following table

query = """
  CREATE TABLE customers2 (
      "customer_id" serial PRIMARY KEY,    
      "name" VARCHAR UNIQUE,
      "name2" VARCHAR UNIQUE,
      "email" VARCHAR NOT NULL,
      "active" bool NOT NULL DEFAULT TRUE,
      constraint test unique(name, name2)
)
"""
cur.execute(query)

Then I am inserting some values

 query = """
    INSERT INTO customers2(name, name2, email)
    VALUES 
       ('IBM', 'a','[email protected]'),
       ('Microsoft','b', '[email protected]'),
       ('Intel', 'c','[email protected]');
 """
 cur.execute(query)

Then I would like to insert a combination of name = "Microsoft" and name2 = "a" which values already exist but not in combination. The following command gives an error

 query = """
   INSERT INTO customers2 (name, name2, email)
   VALUES('Microsoft','a','[email protected]') 
   ON CONFLICT (name, name2) 
   DO UPDATE SET name2 = name2+"b";
 """
 cur.execute(query)
 IntegrityError: UNIQUE constraint failed: customers2.name2 

What is the reason for this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source