title: MySQL Foreign key with duplicate values constraints fails
body: <p>I have a user table with <strong>id</strong> and <strong>name</strong></p>
  
  <pre><code>User Table:
  +----+------+
  | id | Name |
  +----+------+
  |  1 | Dan  |
  +----+------+
  </code></pre>
  
  <p>And a customer Table, each user can have several customers.</p>
  
  <pre><code>Customers Table:
  +----+---------------+-------------+-------+
  | id | customer_id   | user_id     | Name  |
  +----+---------------+-------------+-------+
  |  1 |    1          |       1     | Rito  |
  |  2 |    2          |       1     | Plz   |
  +----+---------------+-------------+-------+
  </code></pre>
  
  <p>I've made user_id an index, and when I try put it as a foreign key with on delete cascade it fails and claims that it is duplicated.
  Both keys are same type, both tables are InnoDB!</p>
  
  <p>This is my request:</p>
  
  <pre><code>ALTER TABLE `customers` 
  ADD CONSTRAINT `user_id_fk`
    FOREIGN KEY (`user_id`)
    REFERENCES `users` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE
  </code></pre>
  
  <p>This is my user table creation code:</p>
  
  <pre><code>CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `Name` varchar(35) CHARACTER SET utf8 NOT NULL,
    PRIMARY KEY (`id`),
  ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  </code></pre>
  
  <p>This is the customer table creation code:</p>
  
  <pre><code>SQL Statement:
  CREATE TABLE `customers` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11) DEFAULT NULL,
    `customer_id` int(11) DEFAULT NULL,
    `Name` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `unique_customer_user` (`customer_id`,`user_id`),
    KEY `user_id_idx` (`user_id`),
  ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
  </code></pre>
  
  <blockquote>
    <p>"ERROR 1022: Can't write; duplicate key in table"</p>
  </blockquote>
  
  <p>My question is, <strong>how can I do that delete cascade in this table (without triggers) using foreign keys?</strong></p>
  
  <p><strong><em>Solved:</em></strong></p>
  
  <p>Apparently the duplicate was related to the constrain <strong>name</strong> and not the values of the data inside the table.</p>
  
  <p><strong>For future readers, You cannot use the same constraint name over the entire database! not just per table.</strong></p>