Search code examples
mysql-error-1062compound-key

Why is this compound primary key not working as expected?


I have a table "tbl_project_user_assignment" with a compound primary key.

It is made up of project_id and user_id

Each of these are also a foreign key to the project and user tables respectively.

At the moment, I have 2 entries in this table as below...

project_id | user_id
--------------------
1          | 1
1          | 2

When I run this sql query...

INSERT INTO tbl_project_user_assignment (project_id, user_id) VALUES (2, 1);

...I get the following error message:

Integrity constraint violation: 1062 Duplicate entry '1' for key 'FK_project_user'

The FK_project_user key is the one linking the project_id to the tbl_project id.

This doesn't make sense to me because the values I'm inserting are unique...

Any ideas?


Solution

  • It looks like FK_project_user is a unique key. Try dropping temporarily that constraint and perform the insert again.

    If insert works, recreate the constraint making sure it's not flagged as unique anymore.