Backpatch of 9918c111 from master. Conflicts: django/db/models/sql/compiler.py tests/model_inheritance_regress/tests.py