title: Mapping Composite primary keys of one table with that of the other table in ManyToMany Relationship
body: <p>I have a situation where I have an Entity named User and another Entity UserHasFriends and there is a many-to-many relationship between the two. For Instance, a user can be friends with many other users and vice versa. I have 2 Primary Keys in the User table, userId and another is uniqueNo, that is associated with the User. In the table UserHasFriends, I have composite primary key(userId, uniqueNo),which are also foreign keys to the User table, that indicates that the user is friends with other users. The table UserHasFriends also contains some other attributes.</p>
  
  <p>The problem is: When i am trying to create this schema, it shows me an error: "Cannot add Foreign Key".</p>
  
  <hr>
  
  <pre><code>CREATE TABLE IF NOT EXISTS `mydb`.`User` (
    `userId` INT NOT NULL,
    `uniqueNo` VARCHAR(45) NOT NULL,
    `username` VARCHAR(45) NOT NULL,
    `password` VARCHAR(45) NOT NULL,
    `email` VARCHAR(45) NOT NULL,
    `firstName` VARCHAR(45) NOT NULL,
    `lastName` VARCHAR(45) NOT NULL,
    `phoneNo` VARCHAR(45) NULL,
    `address` VARCHAR(45) NULL,
    PRIMARY KEY (`userId`, `uniqueNo`))
  ENGINE = InnoDB
  
  
  
  CREATE TABLE IF NOT EXISTS `mydb`.`UserHasFriends` (
    `userId` INT NOT NULL,
    `uniqueNo` VARCHAR(45) NOT NULL,
    `requestStatus` VARCHAR(45) NULL,
    PRIMARY KEY (`userId`, `uniqueNo`))
  ENGINE = InnoDB
  </code></pre>