Note that because dish-names are currently not unique, many duplicates in the database exist.
The following SQL query should, as far as I can tell, update all FK-relationships.
Now to the interesting question:
Should dish_rating.dishID be a FK to dish.dish
```sql
alter table dish_rating
add constraint dish_rating_dish_dish_fk
foreign key (dishID) references dish (dish)
on delete cascade;
# because dishes already have a cafeteria, storing this agiain is not nessesary
alter table dish_rating
drop column cafeteriaID;
```
With relabling as (SELECT COUNT(*), name, cafeteriaID, min(dish) as group_dish_id
FROM dish
GROUP BY name, cafeteriaID
HAVING COUNT(*) > 1
ORDER BY COUNT(*) desc),
dishes_to_relabel as (SELECT DISTINCT dish as old_id, group_dish_id as new_id
FROM relabling r,
dish d
WHERE r.name = d.name
and r.cafeteriaID = d.cafeteriaID
and r.group_dish_id != d.dish)
update dish2dishflags d2f
set dish = (SELECT r.new_id
from dishes_to_relabel r
WHERE r.old_id = d2f.dish);
update dish2mensa d2m
set dish = (SELECT r.new_id
from dishes_to_relabel r
WHERE r.old_id = d2m.dish);
# dish_rating does NOT have a FK-set => intentional?
update dish_rating dr
set dr.dishID = (SELECT r.new_id
from dishes_to_relabel r
WHERE r.old_id = dr.dishID);
update dish_to_dish_name_tag dt
set dt.dishID = (SELECT r.new_id
from dishes_to_relabel r
WHERE r.old_id = dt.dishID);
update dishes_of_the_week dw
set dw.dishID = (SELECT r.new_id
from dishes_to_relabel r
WHERE r.old_id = dw.dishID);
update dish d
set d.dish = (SELECT r.new_id
from dishes_to_relabel r
WHERE r.old_id = d.dish);
During https://github.com/TUM-Dev/Campus-Backend/pull/314#pullrequestreview-1924563994 you noted that updating the type is likely a better idea. This PR implements this change.
Note that because dish-names are currently not unique, many duplicates in the database exist. The following SQL query should, as far as I can tell, update all FK-relationships.
Now to the interesting question:
Should
dish_rating.dishID
be a FK todish.dish
```sql alter table dish_rating add constraint dish_rating_dish_dish_fk foreign key (dishID) references dish (dish) on delete cascade; # because dishes already have a cafeteria, storing this agiain is not nessesary alter table dish_rating drop column cafeteriaID; ```