Repository: mathesar-foundation/mathesar

Issue:
ForeignKey error
I'm not sure what happened, but somehow I got Mathesar into a state where all API requests to fetch the list of tables for my Library schema returned the following error. This made Mathesar unusable. 

<details>
<summary>Traceback</summary>

```
Environment:


Request Method: GET
Request URL: http://localhost:8000/api/db/v0/tables/?schema=3&limit=500

Django Version: 3.1.14
Python Version: 3.9.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'django_filters',
 'django_property_filter',
 'mathesar']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 466, in handle_exception
    response = exception_handler(exc, context)
  File "/code/mathesar/exception_handlers.py", line 55, in mathesar_exception_handler
    raise exc
  File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/rest_framework/mixins.py", line 40, in list
    page = self.paginate_queryset(queryset)
  File "/usr/local/lib/python3.9/site-packages/rest_framework/generics.py", line 171, in paginate_queryset
    return self.paginator.paginate_queryset(queryset, self.request, view=self)
  File "/usr/local/lib/python3.9/site-packages/rest_framework/pagination.py", line 395, in paginate_queryset
    return list(queryset[self.offset:self.offset + self.limit])
  File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 269, in __len__
    self._fetch_all()
  File "/code/mathesar/utils/prefetch.py", line 158, in _fetch_all
    prefetcher.fetch(obj_list, name, self.model, forwarders)
  File "/code/mathesar/utils/prefetch.py", line 270, in fetch
    related_data = self.filter(data_mapping.keys(), data_mapping.values())
  File "/code/mathesar/models/base.py", line 267, in <lambda>
    filter=lambda oids, tables: reflect_tables_from_oids(
  File "/code/db/tables/operations/select.py", line 38, in reflect_tables_from_oids
    table_oids_to_sa_tables[table_oid] = reflect_table(
  File "/code/db/tables/operations/select.py", line 19, in reflect_table
    return Table(name, metadata, schema=schema, autoload_with=autoload_with, extend_existing=True)
  File "<string>", line 2, in __new__
    <source code not available>
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py", line 298, in warned
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 586, in __new__
    table._init_existing(*args, **kw)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 787, in _init_existing
    self._autoload(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 705, in _autoload
    conn_insp.reflect_table(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py", line 779, in reflect_table
    self._reflect_column(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py", line 903, in _reflect_column
    table.append_column(col, replace_existing=True)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 883, in append_column
    column._set_parent_with_dispatch(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/base.py", line 1025, in _set_parent_with_dispatch
    self._set_parent(parent, **kw)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 1774, in _set_parent
    table.foreign_keys.remove(fk)

Exception Type: KeyError at /api/db/v0/tables/
Exception Value: ForeignKey('Library Management.Publishers.id')

```
</details>

Restarting Docker fixed the issue for me.

CC @mathemancer



Discussion:

Resolving patch:
diff --git a/db/columns/utils.py b/db/columns/utils.py
--- a/db/columns/utils.py
+++ b/db/columns/utils.py
@@ -31,7 +31,6 @@ def get_enriched_column_table(table, metadata, engine=None):
         metadata,
         *table_columns,
         schema=table.schema,
-        extend_existing=True,
     )
 
 
diff --git a/mathesar/models/base.py b/mathesar/models/base.py
--- a/mathesar/models/base.py
+++ b/mathesar/models/base.py
@@ -23,6 +23,7 @@
 )
 from db.constraints import utils as constraint_utils
 from db.dependents.dependents_utils import get_dependents_graph, has_dependents
+from db.metadata import get_empty_metadata
 from db.records.operations.delete import delete_record
 from db.records.operations.insert import insert_record_or_records
 from db.records.operations.select import get_column_cast_records, get_count, get_record
@@ -351,7 +352,7 @@ def _enriched_column_sa_table(self):
         return column_utils.get_enriched_column_table(
             table=self._sa_table,
             engine=self._sa_engine,
-            metadata=get_cached_metadata(),
+            metadata=get_empty_metadata(),
         )
 
     @property


Test patch: