diff --git a/workpackages/remapping.py b/workpackages/remapping.py index 89e5910..a26545c 100644 --- a/workpackages/remapping.py +++ b/workpackages/remapping.py @@ -117,7 +117,7 @@ def remap_table_wp_to_master(cursor, table_name, wp_name, new_master_fid): wp_fids_missing = set() sql = ( f"""SELECT {pkey_column_escaped} FROM {table_name_escaped} """ - f"""LEFT JOIN {remap_table} AS mapped ON fid = mapped.wp_fid WHERE mapped.master_fid IS NULL""" + f"""LEFT JOIN {remap_table} AS mapped ON {pkey_column_escaped} = mapped.wp_fid WHERE mapped.master_fid IS NULL""" ) for row in cursor.execute(sql): wp_fids_missing.add(row[0]) @@ -131,7 +131,7 @@ def remap_table_wp_to_master(cursor, table_name, wp_name, new_master_fid): mapping = [] # list of tuples (wp_fid, master_fid) sql = ( f"""SELECT {pkey_column_escaped}, mapped.master_fid FROM {table_name_escaped} """ - f"""LEFT JOIN {remap_table} AS mapped ON fid = mapped.wp_fid""" + f"""LEFT JOIN {remap_table} AS mapped ON {pkey_column_escaped} = mapped.wp_fid""" ) for row in cursor.execute(sql): mapping.append((row[0], row[1])) @@ -141,5 +141,6 @@ def remap_table_wp_to_master(cursor, table_name, wp_name, new_master_fid): for wp_fid, master_fid in mapping: cursor.execute( - f"""UPDATE {table_name_escaped} SET {pkey_column_escaped} = ? WHERE fid = ?""", (master_fid, -wp_fid) + f"""UPDATE {table_name_escaped} SET {pkey_column_escaped} = ? WHERE {pkey_column_escaped} = ?""", + (master_fid, -wp_fid), ) diff --git a/workpackages/wp.py b/workpackages/wp.py index 627e4e0..6a48091 100644 --- a/workpackages/wp.py +++ b/workpackages/wp.py @@ -13,7 +13,7 @@ import yaml from .wp_utils import escape_double_quotes -from .remapping import remap_table_master_to_wp, remap_table_wp_to_master +from .remapping import remap_table_master_to_wp, remap_table_wp_to_master, _table_pkey # Layout of files: # @@ -191,7 +191,9 @@ def _logger_callback(level, text_bytes): for wp_table in wp_config.wp_tables: wp_table_name = wp_table.name wp_tab_name_esc = escape_double_quotes(wp_table_name) - c.execute(f"""SELECT max(fid) FROM {wp_tab_name_esc};""") + pkey_column = _table_pkey(c, wp_table_name) + pkey_column_escaped = escape_double_quotes(pkey_column) + c.execute(f"""SELECT max({pkey_column_escaped}) FROM {wp_tab_name_esc};""") new_master_fid = c.fetchone()[0] if new_master_fid is None: new_master_fid = 1 # empty table so far