ALTER TABLE `UserSessionHasOrder`
DROP FOREIGN KEY `fk_UserSession_has_Order_UserSession1`;

ALTER TABLE `UserSession`
ADD COLUMN `id` INT(10) UNSIGNED NULL DEFAULT NULL FIRST,
DROP PRIMARY KEY;

ALTER TABLE `UserSession`
CHANGE COLUMN `id` `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (`id`);

ALTER TABLE `UserSessionHasOrder`
ADD COLUMN `userSessionId` INT(10) UNSIGNED NULL AFTER `userSessionSid`;

ALTER TABLE `UserSessionHasOrder`
ADD CONSTRAINT `fk_UserSession_has_Order_UserSession1` FOREIGN KEY (`userSessionId`) REFERENCES `UserSession` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION;

set AUTOCOMMIT = 0;
update UserSession u, UserSessionHasOrder uo set uo.userSessionId = u.id where uo.userSessionSid = u.sid;
commit;
set AUTOCOMMIT = 1;

ALTER TABLE `UserSessionHasOrder`
DROP COLUMN `userSessionSid`,
DROP PRIMARY KEY;

ALTER TABLE `UserSession`
ADD UNIQUE INDEX `sid` (`sid`);

ALTER TABLE `UserSessionHasOrder`
DROP FOREIGN KEY `fk_UserSession_has_Order_UserSession1`;

ALTER TABLE `UserSessionHasOrder`
CHANGE COLUMN `userSessionId` `userSessionId` INT(10) UNSIGNED NOT NULL FIRST,
ADD PRIMARY KEY (`userSessionId`, `orderId`);

ALTER TABLE `UserSessionHasOrder`
ADD CONSTRAINT `fk_UserSession_has_Order_UserSession1` FOREIGN KEY (`userSessionId`) REFERENCES `UserSession` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION;

ALTER TABLE `UserSessionHasOrder`
ADD INDEX `userSessionId` (`userSessionId`);