diff --git a/migrations/20240727194157_move_tags_to_join_table.sql b/migrations/20240727194157_move_tags_to_join_table.sql index c18b3d6..cb39e75 100644 --- a/migrations/20240727194157_move_tags_to_join_table.sql +++ b/migrations/20240727194157_move_tags_to_join_table.sql @@ -1,16 +1,16 @@ -- Create a new join table to associate time_entries with tags -CREATE TABLE public.time_entry_tags ( - time_entry_id bigint not null references public.time_entries(id) on delete cascade, - tag_id bigint not null references public.tags(id) on delete cascade, +CREATE TABLE time_entry_tags ( + time_entry_id bigint not null references time_entries(id) on delete cascade, + tag_id bigint not null references tags(id) on delete cascade, primary key (time_entry_id, tag_id) ); -- Insert data into the new join table based on the existing time_entries and tags data -INSERT INTO public.time_entry_tags (time_entry_id, tag_id) +INSERT INTO time_entry_tags (time_entry_id, tag_id) SELECT time_entries.id, UNNEST(time_entries.tag_ids) AS tag_id -FROM public.time_entries +FROM time_entries WHERE time_entries.tag_ids IS NOT NULL; -- Remove the tag_ids array column from the time_entries table -ALTER TABLE public.time_entries +ALTER TABLE time_entries DROP COLUMN tag_ids;