From 67879a5046087d29585ea05a02fe55d745f24213 Mon Sep 17 00:00:00 2001 From: mingbox Date: Sat, 25 Mar 2023 16:12:30 -0700 Subject: [PATCH 1/2] sol: implement the set_field for db migration Use the native sql update operation to fulfill the data migration requirement for set_field --- store/migrations/0002_cell_field.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/store/migrations/0002_cell_field.py b/store/migrations/0002_cell_field.py index 7a0e9d0..71ea7cf 100644 --- a/store/migrations/0002_cell_field.py +++ b/store/migrations/0002_cell_field.py @@ -5,8 +5,16 @@ def set_field(apps, schema_editor): - # Complete this function - raise NotImplementedError + migrations.RunSQL( + """update store.cell set field_id = t.field_id + from ( + SELECT f.id as field_id, c.id as cell_id + FROM store.schema s join store.field f on s.id = f.schema_id + join store.cell c on c.field_name = f.name + join store.record r on c.record_id = r.id and s.id = r.schema_id + ) t + where store.cell.id = t.cell_id""" + ) From e87ac4a03a33faa49c4c2b7e494fb0d70200f4e3 Mon Sep 17 00:00:00 2001 From: mingbox Date: Sat, 25 Mar 2023 23:48:04 -0700 Subject: [PATCH 2/2] update since field_name might not be unique --- store/migrations/0002_cell_field.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/store/migrations/0002_cell_field.py b/store/migrations/0002_cell_field.py index 71ea7cf..aa8d149 100644 --- a/store/migrations/0002_cell_field.py +++ b/store/migrations/0002_cell_field.py @@ -9,11 +9,11 @@ def set_field(apps, schema_editor): """update store.cell set field_id = t.field_id from ( SELECT f.id as field_id, c.id as cell_id - FROM store.schema s join store.field f on s.id = f.schema_id - join store.cell c on c.field_name = f.name - join store.record r on c.record_id = r.id and s.id = r.schema_id + FROM store_field f + join store_record r on f.schema_id = r.schema_id + join store_cell c on c.field_name = f.name and c.record_id = r.id ) t - where store.cell.id = t.cell_id""" + where store_cell.id = t.cell_id""" )