Fix index introspection on PostgreSQL 18.
Review Request #14923 — Created March 17, 2026 and updated — Latest diff uploaded
PostgreSQL 18 stores
NOT NULLconstraints inpg_constraint(with
contype='n'). Django'sget_constraints()includes these in its
results, but with all type flags (index, unique, primary_key, check,
foreign_key) set to False.get_constraints_for_table()was passing
through all constraint entries unconditionally, causing these NOT NULL
constraints to be tracked as regular non-unique indexes in
DatabaseState(e.g., "tests_testmodel_int_field1_not_null").This caused two categories of failures:
ChangeFieldoperations that modifieddb_indexoruniquewould
find unexpectedNOT NULLindexes in the database state, causing
assertion failures.
ChangeMeta(indexes)operations would seeNOT NULLconstraint
entries instead of the actual Django-managed indexes, generating
incorrectDROP/CREATE INDEX SQL.The fix filters
get_constraints_for_table()to only include entries
where index, unique, or primary_key is True. This excludes NOT NULL
constraints, check constraints, and foreign key constraint entries --
none of which should be tracked as indexes in DatabaseState. (Foreign
key fields still have their actual indexes captured via separate
index=Trueentries, and check constraints are handled through Django's
Meta.constraintssystem.)
Ran dbtests-minmax