feat(migration): enforce ownerId requirement in Product table
- Removed all products without an owner to maintain data integrity. - Updated ownerId column to be non-nullable. - Modified foreign key constraint for ownerId to use ON DELETE CASCADE.
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
-- Steg 1: Ta bort alla produkter utan ägare (globala seed-produkter)
|
||||
-- Detta tar automatiskt bort relaterade rader via ON DELETE CASCADE
|
||||
-- för: InventoryItem, PantryItem, ReceiptAlias, ProductTag, Nutrition, UserProduct
|
||||
DELETE FROM `Product` WHERE `ownerId` IS NULL;
|
||||
|
||||
-- Steg 2: Gör ownerId obligatoriskt
|
||||
ALTER TABLE `Product`
|
||||
MODIFY COLUMN `ownerId` INT NOT NULL;
|
||||
|
||||
-- Steg 3: Uppdatera foreign key constraint till CASCADE (ta bort gammal, lägg till ny)
|
||||
ALTER TABLE `Product`
|
||||
DROP FOREIGN KEY IF EXISTS `Product_ownerId_fkey`;
|
||||
|
||||
ALTER TABLE `Product`
|
||||
ADD CONSTRAINT `Product_ownerId_fkey`
|
||||
FOREIGN KEY (`ownerId`) REFERENCES `User`(`id`)
|
||||
ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
Reference in New Issue
Block a user