Repository: mathesar-foundation/mathesar

Issue:
Summarization suggestion aggregates columns instead of grouping, when the base column is a unique key column
## Description
* Select a table with a unique key column as the base table in Data Explorer Eg., Patrons
* Add the unique key column, along with a few other columns. Eg., Email, first name, last name
* Summarize by the unique key column. Expect the other columns to be grouped, instead notice that they are aggregated as a list.

Related comment/issue: https://github.com/centerofci/mathesar/issues/2145#issuecomment-1362358413

>  _Summarize based on patron email (there should be LESS than 180 rows at this point, and only "Book Title" should show up as a List)_
> For this point, we'd expect patron first name and last name to be part of the grouped columns, instead they are getting aggregated as list. Summarization suggestion aggregates them instead of grouping.
> Currently, the list has a single value but it's still an array column so it appears inside a pill, which looks like this:
![Screenshot 2022-12-22 at 8 47 29 AM](https://user-images.githubusercontent.com/11850603/209048160-425bcbd6-38d1-41ed-af12-ec98e35f5816.png)
> What we'd expect is this:
![Screenshot 2022-12-22 at 8 47 45 AM](https://user-images.githubusercontent.com/11850603/209048184-d86308c9-1775-4edd-8928-57c697053f87.png)



Discussion:
@dmos62 please prioritize this, it's a nice to have for the demo video. 

Resolving patch:
diff --git a/db/records/operations/select.py b/db/records/operations/select.py
--- a/db/records/operations/select.py
+++ b/db/records/operations/select.py
@@ -83,7 +83,6 @@ def get_records(
         search=search,
         duplicate_only=duplicate_only,
     )
-    print(str(relation))
     return execute_pg_query(engine, relation)
 
 
diff --git a/db/transforms/operations/finish_specifying.py b/db/transforms/operations/finish_specifying.py
--- a/db/transforms/operations/finish_specifying.py
+++ b/db/transforms/operations/finish_specifying.py
@@ -1,3 +1,5 @@
+from sqlalchemy import inspect
+
 from db.tables.operations import select as tables_select
 from db.tables.operations.select import get_joinable_tables
 from db.transforms.base import Summarize
@@ -172,14 +174,20 @@ def _is_initial_column_unique_constrained(initial_column, engine, metadata):
         engine=engine,
         metadata=metadata,
     )
-    return _is_sa_column_unique_constrained(sa_column)
-
-
-def _is_sa_column_unique_constrained(sa_column):
-    return bool(
-        sa_column.primary_key
-        or sa_column.unique
-    )
+    return _is_sa_column_unique_constrained(sa_column, engine)
+
+
+def _is_sa_column_unique_constrained(sa_column, engine):
+    is_primary_key = bool(sa_column.primary_key)
+    if is_primary_key:
+        return True
+    table_name = sa_column.table.name
+    schema_name = sa_column.table.schema
+    unique_constraints = inspect(engine).get_unique_constraints(table_name, schema_name)
+    for unique_constraint in unique_constraints:
+        if sa_column.name in unique_constraint['column_names']:
+            return True
+    return False
 
 
 def _get_oid_of_initial_column(initial_column):


Test patch: