1

I want to create two table AdminSessions and UserSessions to store the client session data. Both of them have a pk named sessionKey with the properties of PK, NN, UQ. When I try to do a forward engineering, I encounter Can't write; duplicate key in table 'UserSessions'. After reading this solution, I try to change the name of the pks to sessionKeya and sessionKeyb but it does not work.

How to solve this error?

Error Message

ERROR: Error 1022: Can't write; duplicate key in table 'UserSessions'

enter image description here enter image description here

SQL

    CREATE TABLE IF NOT EXISTS `CodeSpace`.`UserSessions` (
      `sessionKey` VARCHAR(128) NOT NULL,
      `userId` INT UNSIGNED NOT NULL,
      `lastAccessTime` DATETIME NULL,
      `ip` VARCHAR(45) NULL,
      `expiryTime` DATETIME NULL,
      PRIMARY KEY (`sessionKey`),
      UNIQUE INDEX `userId_UNIQUE` (`userId` ASC),
      UNIQUE INDEX `id_UNIQUE` (`sessionKey` ASC),
      CONSTRAINT `fk_UserSession_1`
        FOREIGN KEY (`userId`)
        REFERENCES `CodeSpace`.`Users` (`id`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB
Community
  • 1
  • 1
Casper
  • 4,435
  • 10
  • 41
  • 72
  • The problem is not with the columnname of the primary key, it is very likely the name of the foreign key, `fk_UserSession_1`, you probably use this name somewhere else. Rename it (or look for the duplicate with e.g. `select * from information_schema.innodb_sys_foreign where id like '%fk_UserSession_1';` and rename it there, because its name fits to this table) – Solarflare Jul 24 '16 at 09:24

0 Answers0