Repository: mathesar-foundation/mathesar

Issue:
/map_imported_columns/ API throwing 500 error
## Description
<!-- A clear and concise description of what the bug is. -->
Sending a `POST` request with the request body `"import_target": <existing_table_id>` to `/api/db/v0/tables/<temp_table_id>/map_imported_columns/` is expected to provide an array in the response body that contains suggested mappings of temp_table column IDs to target_table column IDs. However, a 500 Internal Server Error is currently being returned instead.
## Expected behavior
<!-- A clear and concise description of what you expected to happen. -->

The `map_imported_columns` API should return an array in the response body that contains suggested mappings of temp_table column IDs to target_table column IDs

## To Reproduce
<!-- How can we recreate this bug? Please try to provide a Minimal, Complete, and Verifiable (http://stackoverflow.com/help/mcve) example if code-related. -->

Create two tables with identical column names.

For example:

Table 1:
![image](https://user-images.githubusercontent.com/92625518/227785048-247226fa-dd6c-4c05-8504-80501589ab54.png)

Table 2:
![image](https://user-images.githubusercontent.com/92625518/227785150-491c79c5-3c27-4a0a-8b87-469bbcf045ae.png)

Send a `POST` request to `/api/db/v0/tables/<temp_table_id>/map_imported_columns/` with temp_table_id as table 2 id and existing_table_id as table 1 id.

Expect to get an array in the response body that contains suggested mappings of temp_table column IDs to target_table column IDs. But instead receive 500 Internal Server Error.
 
![image](https://user-images.githubusercontent.com/92625518/227785672-fa505a28-a519-4645-a0dd-e8bf8da51c5e.png)

I am currently working on this issue.



Discussion:

Resolving patch:
diff --git a/mathesar/api/db/viewsets/tables.py b/mathesar/api/db/viewsets/tables.py
--- a/mathesar/api/db/viewsets/tables.py
+++ b/mathesar/api/db/viewsets/tables.py
@@ -247,9 +247,11 @@ def existing_import(self, request, pk=None):
     @action(methods=['post'], detail=True)
     def map_imported_columns(self, request, pk=None):
         temp_table = self.get_object()
-        target_table = request.get('import_target', None)
+        target_table_id = request.data.get('import_target', None)
+        target_table = get_table_or_404(target_table_id)
         try:
-            temp_table.suggest_col_mappings_for_import(target_table)
+            mappings = temp_table.suggest_col_mappings_for_import(target_table)
+            return Response({"mappings": mappings})
         except ColumnMappingsNotFound as e:
             raise database_api_exceptions.ColumnMappingsNotFound(
                 e,
diff --git a/mathesar/models/base.py b/mathesar/models/base.py
--- a/mathesar/models/base.py
+++ b/mathesar/models/base.py
@@ -696,7 +696,22 @@ def insert_records_to_existing_table(self, existing_table, data_files, mappings=
     def suggest_col_mappings_for_import(self, existing_table):
         temp_table_col_list = self.get_column_name_type_map()
         target_table_col_list = existing_table.get_column_name_type_map()
-        return column_utils.find_match(temp_table_col_list, target_table_col_list, self._sa_engine)
+        temp_table_name_id_map = self.get_column_name_id_bidirectional_map()
+        target_table_name_id_map = existing_table.get_column_name_id_bidirectional_map()
+        column_mappings = column_utils.find_match(temp_table_col_list, target_table_col_list, self._sa_engine)
+
+        # Convert python list indices to django ids.
+        mappings = [
+            (
+                temp_table_name_id_map[
+                    temp_table_col_list[from_col][0]  # from_column name
+                ],
+                target_table_name_id_map[
+                    target_table_col_list[target_col][0]  # target_column name
+                ]
+            ) for from_col, target_col in column_mappings
+        ]
+        return mappings
 
 
 class Column(ReflectionManagerMixin, BaseModel):


Test patch: