Skip to content

Potential for errors when deprecating non-nullable fields #17

@LincolnPuzey

Description

@LincolnPuzey

Say in version N of my project I have this model and code that assumes foo_instance.text_field is always going to be a string.

class FooModel(models.Model):
    text_field = models.CharField(max_length=256)

Then in version N+1 I then deprecate it

class FooModel(models.Model):
    text_field = deprecate_field(models.CharField(max_length=256), "")

Then when version N and N+1 are running simultaneously:

  1. version N+1 inserts a row, it will have text_field=NULL
  2. version N selects this row, and assumes text_field is going to be a string, when it is actually None
  3. Potentially raising a TypeError / ValueError

I'm not sure how this could be addressed by the django-deprecate-fields library. Maybe this just needs to be detailed in the docs.

For us the solution is to first use a AddDefaultValue operation on the field to add a database-level default. This is so the rows inserted by N+1 get this default value instead of NULL, so version N can still handle them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions