Opened 7 years ago
Closed 7 years ago
#29185 closed Bug (duplicate)
Migrations with `default=some_callable` populate all existing rows with the same value
Reported by: | Stavros Korokithakis | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 2.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
A migration that adds a field with default=some_func
run that function once and then use the value as the default for the new rows. This fact is [documented](https://docs.djangoproject.com/en/2.0/howto/writing-migrations/#migrations-that-add-unique-fields), though still surprising.
I propose that makemigrations
either issues a warning when a field with a default=callable
is encountered, or, better yet, that it asks what the user intends and generates the three-step migration in the documentation instead. An acceptable solution would also be to generate the three-step migration without asking and then warn about slowness, as that is more in line with users' expectations.
Change History (3)
follow-up: 2 comment:1 by , 7 years ago
comment:2 by , 7 years ago
Replying to James Pic:
I don't see why a Django user would add a unique=True,default=callable to a model, and not have a 3 operation migration generated out of the box:
The only reason I can see if the function is something like datetime.now
and the user doesn't want to incur the performance hit. In that case, however, the user could still optimize for speed by editing the migration. I do agree that the three-step migration is the sane default.
comment:3 by , 7 years ago
Component: | Uncategorized → Migrations |
---|---|
Resolution: | → duplicate |
Status: | new → closed |
Summary: | Migrations with `default=some_callable` exhibit surprising behaviour → Migrations with `default=some_callable` populate all existing rows with the same value |
Duplicate of #23408
I don't see why a Django user would add a unique=True,default=callable to a model, and not have a 3 operation migration generated out of the box:
In terms of software design, this is what i would call a "sane default".
Hence I think this would be a great contribution, let's see what Django experts think about it.