From d2ebc4f182b394fde1f08aa26a7efdf810693f7b Mon Sep 17 00:00:00 2001 From: chihlasm Date: Thu, 9 Apr 2026 17:30:05 +0000 Subject: [PATCH] fix: correct tree tags subquery in template_trees migration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The INSERT into template_trees incorrectly referenced `tags` as a column on the `trees` table. Tags are a relationship via the `tree_tag_assignments` join table — there is no direct column. Migration was failing with: UndefinedColumn: column "tags" does not exist ... FROM trees Fixed by replacing COALESCE(tags, '[]') with a correlated subquery that aggregates tag names from tree_tag_assignments → tree_tags. Co-Authored-By: Claude Sonnet 4.6 --- ...40fe11b427_create_global_content_tables.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py b/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py index 7bf8bbba..01dbac47 100644 --- a/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py +++ b/backend/alembic/versions/3a40fe11b427_create_global_content_tables.py @@ -51,16 +51,26 @@ def upgrade() -> None: op.create_index('ix_platform_steps_step_type', 'platform_steps', ['step_type']) # ── Copy is_default=TRUE trees → template_trees ───────────────────────── + # Note: trees.tags is a relationship via tree_tags join table — no direct column. + # Aggregate tag names via a correlated subquery. op.execute(""" INSERT INTO template_trees (id, name, description, category, tree_type, tree_structure, tags, is_active, created_at, updated_at, source_tree_id) SELECT - gen_random_uuid(), name, description, category, tree_type, - tree_structure, COALESCE(tags, '[]'::jsonb), is_active, - COALESCE(created_at, NOW()), COALESCE(updated_at, NOW()), id - FROM trees - WHERE is_default = TRUE + gen_random_uuid(), t.name, t.description, t.category, t.tree_type, + t.tree_structure, + COALESCE( + (SELECT jsonb_agg(tt.name ORDER BY tt.name) + FROM tree_tag_assignments ta + JOIN tree_tags tt ON tt.id = ta.tag_id + WHERE ta.tree_id = t.id), + '[]'::jsonb + ), + t.is_active, + COALESCE(t.created_at, NOW()), COALESCE(t.updated_at, NOW()), t.id + FROM trees t + WHERE t.is_default = TRUE """) # ── Copy visibility='public' steps → platform_steps ─────────────────────