Repository: mathesar-foundation/mathesar

Issue:
Don't validate for existing column names if they're being moved
<img width="905" alt="Screen Shot 2022-12-21 at 6 02 20 PM" src="https://user-images.githubusercontent.com/287034/209019919-a8dd4cab-4ce9-4c85-8956-3163409e713b.png">

We're removing the `Publisher` column here, so it's okay that the replacement column will be named `Publisher`.


Discussion:

Resolving patch:
diff --git a/db/tables/operations/split.py b/db/tables/operations/split.py
--- a/db/tables/operations/split.py
+++ b/db/tables/operations/split.py
@@ -2,8 +2,11 @@
 
 from db import constants
 from db.columns.base import MathesarColumn
-from db.columns.operations.alter import batch_alter_table_drop_columns
-from db.columns.operations.select import get_column_names_from_attnums
+from db.columns.operations.alter import batch_alter_table_drop_columns, rename_column
+from db.columns.operations.select import (
+    get_column_attnum_from_name,
+    get_column_names_from_attnums,
+)
 from db.links.operations.create import create_foreign_key_link
 from db.tables.operations.create import create_mathesar_table
 from db.tables.operations.select import get_oid_from_table, reflect_table, reflect_table_from_oid
@@ -18,6 +21,11 @@ def _create_split_tables(extracted_table_name, extracted_columns, remainder_tabl
         engine,
     )
     fk_column_name = fk_column_name if fk_column_name else f"{extracted_table.name}_{constants.ID}"
+    extracted_column_names = [
+        col.name for col in extracted_columns
+    ]
+    if fk_column_name in extracted_column_names:
+        fk_column_name = f"mathesar_temp_{fk_column_name}"
     remainder_table_oid = get_oid_from_table(remainder_table_name, schema, engine)
     extracted_table_oid = get_oid_from_table(extracted_table_name, schema, engine)
     create_foreign_key_link(engine, schema, fk_column_name, remainder_table_oid, extracted_table_oid)
@@ -97,6 +105,9 @@ def extract_columns_from_table(old_table_oid, extracted_column_attnums, extracte
             for column_attnum in extracted_column_attnums
         ]
         batch_alter_table_drop_columns(remainder_table_oid, deletion_column_data, conn, engine)
+        fk_column_attnum = get_column_attnum_from_name(remainder_table_oid, fk_column_name, engine, get_empty_metadata())
+        if relationship_fk_column_name != fk_column_name:
+            rename_column(remainder_table_oid, fk_column_attnum, engine, conn, relationship_fk_column_name)
     return extracted_table, remainder_table_with_fk_column, fk_column_name
 
 


Test patch:
diff --git a/mathesar/tests/api/test_table_api.py b/mathesar/tests/api/test_table_api.py
--- a/mathesar/tests/api/test_table_api.py
+++ b/mathesar/tests/api/test_table_api.py
@@ -1564,7 +1564,7 @@ def test_table_extract_columns_specify_fk_column_name(create_patents_table, clie
     column_name_id_map = table.get_column_name_id_bidirectional_map()
     column_names_to_extract = ['Patent Number', 'Title', 'Patent Expiration Date']
     column_ids_to_extract = [column_name_id_map[name] for name in column_names_to_extract]
-    relationship_fk_column_name = "patent_info"
+    relationship_fk_column_name = "Patent Number"
     extract_table_name = "Patent Info"
     split_data = {
         'extract_columns': column_ids_to_extract,