Skip to content

More strict .update validation for non-nullable fields #3348

@UnknownPlatypus

Description

@UnknownPlatypus

Blocked by #3317

Bug report

As said in the comment below,MyModel.objects.all().update(id=None) should raise an error.
At runtime, it would be an IntegrityError: (1048, "Column 'id' cannot be null")

It affects:

  • Field(primary_key=True)
  • Field(null=False)
-   case: autofield_can_be_set_to_none
    main: |
        from myapp.models import MyModel, MyModelExplicitPK
        m = MyModel()
        m.id = 3
        m.id = None
        m2 = MyModel(id=None)
        MyModel.objects.create(id=None)
        MyModel.objects.all().update(id=None) # Should give an error since there's a not-null constraint

        def foo(a: int) -> bool:
          return True
        m2 = MyModel()
        foo(m2.id)

        # At runtime, this would be an error, unless m.save() was called to populate the `id` field.
        # but the plugin cannot catch this.
        foo(m.id)

        exp = MyModelExplicitPK()
        exp.id = 3
        exp.id = None
    installed_apps:
        - myapp
    files:
        -   path: myapp/__init__.py
        -   path: myapp/models.py
            content: |
                from django.db import models
                class MyModel(models.Model):
                    pass
                class MyModelExplicitPK(models.Model):
                    id = models.AutoField(primary_key=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions