Improve writing migrations guide to adding unique fields on existing table
— at Version 3
- The current approach splits the work across three files: one to add the field, one to populate values, and one to restore the constraint. All three operations can be placed in a single migration, which is simpler to follow and has the added benefit of being atomic — removing the race condition that is currently warned about in the docs.
- I think the section should mention that
Field.db_default avoids this problem entirely by having the database generate a unique value per row. This is worth noting upfront so readers can choose the simpler path where their use case allows.
- No mention of performant alternatives for large tables: The
RunPython example iterates row by row with individual saves. For large tables this will be very slow. The docs should note that QuerySet.bulk_update() or RunSQL are worth considering in that case.
Change History
(3)
| Cc: |
MANAS MADESHIYA added
|
| Cc: |
Shai Berger Markus Holtermann added
|
| Description: |
modified (diff)
|
| Summary: |
Improve "Writing migrations" how-to -- unique fields and ManyToManyField through models → Improve writing migrations guide to adding unique fields on existing table
|
| Type: |
Bug → Cleanup/optimization
|
| Version: |
6.0 → dev
|
The docs on "Migrations that add unique fields" was added as part of #23932, I will cc some folks involved in that ticket in case they want to add their opinion
Clifford please can you split this into 2 tickets? Repurpose this ticket only for the "Migrations that add unique fields" suggested changes and a new ticket for "Changing a ManyToManyField to use a through model" suggested changes