ALTER TABLE `InventoryItem`
  ADD COLUMN `userId` INT NULL;

UPDATE `InventoryItem` AS ii
JOIN `Product` AS p ON p.`id` = ii.`productId`
SET ii.`userId` = p.`ownerId`
WHERE ii.`userId` IS NULL;

SET @fallback_user_id := (SELECT `id` FROM `User` ORDER BY `id` ASC LIMIT 1);
UPDATE `InventoryItem`
SET `userId` = @fallback_user_id
WHERE `userId` IS NULL;

ALTER TABLE `InventoryItem`
  MODIFY COLUMN `userId` INT NOT NULL,
  ADD INDEX `InventoryItem_userId_idx` (`userId`),
  ADD CONSTRAINT `InventoryItem_userId_fkey`
    FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
