Opened 4 weeks ago

Last modified 4 weeks ago

#37031 assigned Cleanup/optimization

Improve writing migrations guide to adding unique fields on existing table — at Version 3

Reported by: Clifford Gama Owned by: Clifford Gama
Component: Documentation Version: dev
Severity: Normal Keywords: migrations
Cc: MANAS MADESHIYA, Shai Berger, Markus Holtermann Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Clifford Gama)

  1. 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.
  1. 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.
  1. 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)

comment:1 by MANAS MADESHIYA, 4 weeks ago

Cc: MANAS MADESHIYA added

comment:2 by Sarah Boyce, 4 weeks ago

Cc: Shai Berger Markus Holtermann added

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

comment:3 by Clifford Gama, 4 weeks ago

Description: modified (diff)
Summary: Improve "Writing migrations" how-to -- unique fields and ManyToManyField through modelsImprove writing migrations guide to adding unique fields on existing table
Type: BugCleanup/optimization
Version: 6.0dev
Note: See TracTickets for help on using tickets.
Back to Top