title: MySQL : Error Code: 1215. Cannot add foreign key constraint
body: <p>I have two tables:</p>
  
  <pre><code>Table: utenti
  Columns:
  userId varchar(255) PK 
  password varchar(255)
  
  Table: agenzie
  Columns:
  agenziaId varchar(255) PK 
  userId varchar(255)
  </code></pre>
  
  <p>When I try to create the foreign key on agenzie, this error message appear:</p>
  
  <pre><code>ALTER TABLE agenzie ADD FOREIGN KEY (userId) REFERENCES utenti(userId)  Error Code: 1215. Cannot add foreign key constraint 0.015 sec
  </code></pre>
  
  <p>How can I fix that?</p>
  
  <p>Thank you.</p>
  
  <p>EDIT:</p>
  
  <pre><code>DROP TABLE IF EXISTS `utenti`;
  /*!40101 SET @saved_cs_client     = @@character_set_client */;
  /*!40101 SET character_set_client = utf8 */;
  CREATE TABLE `utenti` (
  `userId` varchar(255) CHARACTER SET latin1 NOT NULL,
  `password` varchar(255) CHARACTER SET latin1 NOT NULL,
  PRIMARY KEY (`userId`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  
  DROP TABLE IF EXISTS `agenzie`;
  /*!40101 SET @saved_cs_client     = @@character_set_client */;
  /*!40101 SET character_set_client = utf8 */;
  CREATE TABLE `agenzie` (
  `agenziaId` varchar(255) NOT NULL,
  `userId` varchar(255) NOT NULL,
  PRIMARY KEY (`agenziaId`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  </code></pre>