Opened 4 years ago
Closed 4 years ago
#32495 closed Bug (duplicate)
Migrations that modify constraints raise an exception on multi-schema databases
Reported by: | Chris Spencer | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 3.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have a multi-tenant Django application with a PostgreSQL backend that separates user data using uniquely named schemas. Each tenant has a separate front-end website and settings file, specifying their unique database connection parameters. The schema structures are identical and are managed and migrated the same way.
I'm finding this setup is not fully supported by Django, in that I'm unable to migrate any changes to indexes or constraints.
The query that retrieves the old SQL object to update (django.db.backends.postgresql.introspection.DatabaseIntrospection.get_constraints) retrieves any object matching the name. The problem is that it's able to see across any schema the current user has access to, and not just the current schema being migrated, resulting in an error like:
ValueError: Found wrong number (7) of constraints for myapp_mytable(col1, col2, col3)
i.e. This error would be thrown if I had 7 schemas on a database, all containing the same constraint, and I tried to migrate one of the schemas.
I've been able to work around this issue by monkey patching the get_constraints method using a variation of my submitted patch (https://github.com/django/django/pull/14067), which just updates the query to filter by the current schema.
Yes it's not supported, official support for database schemas is discussed in #6148.