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)

comment:1 by James Pic, 7 years ago

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:

  1. add null=True,unique=False,
  2. provision new column on each row with the callback,
  3. set null=False,unique=True


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.

in reply to:  1 comment:2 by Stavros Korokithakis, 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 Tim Graham, 7 years ago

Component: UncategorizedMigrations
Resolution: duplicate
Status: newclosed
Summary: Migrations with `default=some_callable` exhibit surprising behaviourMigrations with `default=some_callable` populate all existing rows with the same value

Duplicate of #23408

Note: See TracTickets for help on using tickets.
Back to Top