How to get Foreign Keys Horribly Wrong in Django
summary
Django silently creates indexes for ForeignKey fields and migrations often respond by dropping and recreating constraints (which forces a full check of parent rows), so inspect the SQL output, make migrations reversible by providing reverse SQL, and use SeparateDatabaseAndState to run only a drop index concurrently on the DB while changing state. For heavy traffic split changes into a non-transactional migration (atomic = False) or run an explicit add_index_concurrently and prefer partial index for sparsely populated audit columns (example: 1,000,000 products, partial index shrinks from 7 MB to 32 KB).
AI-written summary · about
