Repository: mathesar-foundation/mathesar

Issue:
DB Types in column.valid_target_types are not in sync with the types returned in database types endpoint
## Description
* `valid_target_types` property of column returns "DOUBLE PRECISION"
   - Endpoint: /api/v0/tables/14/columns/
* Types endpoint returns mathesar types where Number has the db type "DOUBLE_PRECISION"
   - http://localhost:8000/api/v0/databases/1/types/
   - Mathesar type: Number

Note that "DOUBLE PRECISION" and "DOUBLE_PRECISION" differ from each other.

## Expected behavior
Both endpoints should return values with same spelling.



Discussion:

Resolving patch:
diff --git a/db/types/base.py b/db/types/base.py
--- a/db/types/base.py
+++ b/db/types/base.py
@@ -87,8 +87,8 @@ def get_available_types(engine):
 
 
 def get_db_type_name(sa_type, engine):
-    USER_DEFINED_STR = 'user_defined'
-    db_type = sa_type.__visit_name__
-    if db_type == USER_DEFINED_STR:
-        db_type = sa_type().compile(engine.dialect)
+    try:
+        db_type = sa_type.compile(dialect=engine.dialect)
+    except TypeError:
+        db_type = sa_type().compile(dialect=engine.dialect)
     return db_type


Test patch:
diff --git a/db/tests/columns/operations/test_alter.py b/db/tests/columns/operations/test_alter.py
--- a/db/tests/columns/operations/test_alter.py
+++ b/db/tests/columns/operations/test_alter.py
@@ -425,7 +425,7 @@ def test_batch_update_columns_no_changes(engine_email_type):
 
     assert len(table.columns) == len(updated_table.columns)
     for index, column in enumerate(table.columns):
-        new_column_type = get_db_type_name(updated_table.columns[index].type, engine_email_type)
+        new_column_type = get_db_type_name(updated_table.columns[index].type, engine)
         assert new_column_type == 'VARCHAR'
         assert updated_table.columns[index].name == table.columns[index].name
 
@@ -436,15 +436,15 @@ def test_batch_update_column_names(engine_email_type):
     table_oid = get_oid_from_table(table.name, schema, engine)
 
     column_data = _get_pizza_column_data()
-    column_data[1]['name'] == 'Pizza Style'
-    column_data[2]['name'] == 'Eaten Recently?'
+    column_data[1]['name'] = 'Pizza Style'
+    column_data[2]['name'] = 'Eaten Recently?'
 
     batch_update_columns(table_oid, engine, column_data)
     updated_table = reflect_table(table.name, schema, engine)
 
     assert len(table.columns) == len(updated_table.columns)
     for index, column in enumerate(table.columns):
-        new_column_type = get_db_type_name(updated_table.columns[index].type, engine_email_type)
+        new_column_type = get_db_type_name(updated_table.columns[index].type, engine)
         assert new_column_type == column_data[index]['plain_type']
         assert updated_table.columns[index].name == column_data[index]['name']
 
@@ -455,15 +455,15 @@ def test_batch_update_column_types(engine_email_type):
     table_oid = get_oid_from_table(table.name, schema, engine)
 
     column_data = _get_pizza_column_data()
-    column_data[0]['plain_type'] == 'INTEGER'
-    column_data[2]['plain_type'] == 'BOOLEAN'
+    column_data[0]['plain_type'] = 'DOUBLE PRECISION'
+    column_data[2]['plain_type'] = 'BOOLEAN'
 
     batch_update_columns(table_oid, engine, column_data)
     updated_table = reflect_table(table.name, schema, engine)
 
     assert len(table.columns) == len(updated_table.columns)
     for index, column in enumerate(table.columns):
-        new_column_type = get_db_type_name(updated_table.columns[index].type, engine_email_type)
+        new_column_type = get_db_type_name(updated_table.columns[index].type, engine)
         assert new_column_type == column_data[index]['plain_type']
         assert updated_table.columns[index].name == column_data[index]['name']
 
@@ -474,17 +474,17 @@ def test_batch_update_column_names_and_types(engine_email_type):
     table_oid = get_oid_from_table(table.name, schema, engine)
 
     column_data = _get_pizza_column_data()
-    column_data[0]['name'] == 'Pizza ID'
-    column_data[0]['plain_type'] == 'INTEGER'
-    column_data[1]['name'] == 'Pizza Style'
-    column_data[2]['plain_type'] == 'BOOLEAN'
+    column_data[0]['name'] = 'Pizza ID'
+    column_data[0]['plain_type'] = 'INTEGER'
+    column_data[1]['name'] = 'Pizza Style'
+    column_data[2]['plain_type'] = 'BOOLEAN'
 
     batch_update_columns(table_oid, engine, column_data)
     updated_table = reflect_table(table.name, schema, engine)
 
     assert len(table.columns) == len(updated_table.columns)
     for index, column in enumerate(table.columns):
-        new_column_type = get_db_type_name(updated_table.columns[index].type, engine_email_type)
+        new_column_type = get_db_type_name(updated_table.columns[index].type, engine)
         assert new_column_type == column_data[index]['plain_type']
         assert updated_table.columns[index].name == column_data[index]['name']
 
@@ -503,7 +503,7 @@ def test_batch_update_column_drop_columns(engine_email_type):
 
     assert len(updated_table.columns) == len(table.columns) - 2
     for index, column in enumerate(updated_table.columns):
-        new_column_type = get_db_type_name(updated_table.columns[index].type, engine_email_type)
+        new_column_type = get_db_type_name(updated_table.columns[index].type, engine)
         assert new_column_type == column_data[index - 2]['plain_type']
         assert updated_table.columns[index].name == column_data[index - 2]['name']
 
@@ -525,6 +525,6 @@ def test_batch_update_column_all_operations(engine_email_type):
 
     assert len(updated_table.columns) == len(table.columns) - 1
     for index, column in enumerate(updated_table.columns):
-        new_column_type = get_db_type_name(updated_table.columns[index].type, engine_email_type)
+        new_column_type = get_db_type_name(updated_table.columns[index].type, engine)
         assert new_column_type == column_data[index]['plain_type']
         assert updated_table.columns[index].name == column_data[index]['name']