Exclude views from get_table_names() - #310
gordthompson merged 4 commits into
Conversation
get_table_names() read every row of information_schema.tables for the schema, so views were returned as tables as well as by get_view_names(). SQLAlchemy defines get_table_names() as base tables only, and callers that list tables and views separately showed each view twice. Filter on table_type = 'BASE TABLE'. has_table() previously relied on get_table_names(); it now checks all relations directly, so it keeps returning True for views as SQLAlchemy 2.0 specifies.
gordthompson
left a comment
There was a problem hiding this comment.
We can now extend SQLAlchemy's upstream code. Please review and merge
get_table_names() called the upstream implementation with schema=None, so get_table_names(schema=...) listed the default schema's tables. Pass the requested schema through, and only drop the default-schema entries in multi_entries_to_ignore when no schema was requested, as get_multi_foreign_keys() does. get_multi_foreign_keys() likewise now forwards the caller's postgresql_ignore_search_path instead of False.
|
@gordthompson Thanks, that's much cleaner. I've brought your commit (74df703) into this branch unchanged. Running the suite against it turned up one problem: Full suite at 664ebf6 on CockroachDB v26.3 (psycopg2 / asyncpg / psycopg): 929 / 925 / 929 passed, no failures. The description is updated to match. |
get_table_names()selected every row ofinformation_schema.tablesfor the schema, so views were returned as tables in addition to being returned byget_view_names(). SQLAlchemy definesget_table_names()as base tables only; applications that list tables and views separately show each view twice.Following review, the dialect now extends the upstream PostgreSQL reflection instead of replacing it (74df703, from @gordthompson):
get_table_names()calls the upstream implementation (base tables only) and drops themulti_entries_to_ignoreentries when no schema is requested. The pre-v2SHOW TABLESbranch and thehas_table()override are removed, sohas_table()is upstream's and still returnsTruefor views, as SQLAlchemy 2.0 specifies.get_multi_foreign_keys()drops themulti_entries_to_ignoreentries from the upstream result when no schema is requested.schemastest requirement is open, so the suite's schema-qualified tests run.ComputedReflectionTestand the schema variant oftest_metadataare skipped, and the dev requirements are refreshed.664ebf6 passes the requested
schemaand the caller'spostgresql_ignore_search_paththrough to the upstream calls. Previously they were hard-coded toNone/False, soget_table_names(schema="test_schema")listed the default schema's tables. ACHANGES.mdentry is under the unreleased 2.0.5 section.Tests (
test/test_introspection.py::TableNamesTest):test_get_table_names_excludes_views,test_has_table_includes_views,test_get_table_names_uses_requested_schemaandtest_get_foreign_keys_passes_ignore_search_path. The last two fail on 74df703 and pass on 664ebf6.Full suite at 664ebf6 against CockroachDB
latest-v26.3(single node,test_schema/test_schema_2created as in CI), SQLAlchemy 2.0.54, Python 3.10:For comparison,
masterwith psycopg2 runs 731 passed, 705 skipped (the schema tests are not enabled there). flake8 (tox -e lintsettings) is clean.