﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30644	Postgres introspection don't filter on visible objects.	Georgi Yanchev	Georgi Yanchev	"Several instances of my Django application share the same Postgres database (each in own schema). To specify which schema to use I have in my Django settings:

{{{
DATABASES[default']['OPTIONS'] = {
            'options': '-c search_path={}'.format(get_env('POSTGRES_SCHEMA', 'public')),
        }
}}}

The problem is that migrations fail, because methods like `get_constraints` return constraints from other schemas: https://github.com/django/django/blob/a3417282ac0464a9a2d1d7685bcfef10feed2597/django/db/backends/postgresql/introspection.py#L145

When I look deeper into that code, it looks that there is a mess with schemas filtering.

1. The first SQL query in `get_constraints` will use hardcoded schema name `public`: https://github.com/django/django/blob/a3417282ac0464a9a2d1d7685bcfef10feed2597/django/db/backends/postgresql/introspection.py#L175
2. The second SQL in the same method doesn't filter by schema name, so it will return entries from all schemas: https://github.com/django/django/blob/a3417282ac0464a9a2d1d7685bcfef10feed2597/django/db/backends/postgresql/introspection.py#L214
3. `get_table_list` properly filters by using `pg_catalog.pg_table_is_visible`.
4. `get_sequences` and `get_relations` will return entries from all schemas.

"	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed	Postgres schemas	Georgi Yanchev	Accepted	1	0	0	0	0	0
