Fixed #23947 -- Django's test suite doesn't pass in reverse order with PostgreSQL

Some suggestions to fix the problem when running Django's test suite using PostgreSQL database. With these changes, tests using Sqlite and PostgreSQL are running ok in both reverse and normal order.