Skip to content

How to Handle Non Nullable and Default Value

W. Bomar edited this page Oct 14, 2021 · 1 revision

Related Error

You are trying to add a non-nullable field '...'
to choice without a default; we can't do that
(the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option:

Explanations

Solutions

Key to Icons
  • ☑ Tested.
  • ⚠ May be dangerous.

For cmsplugin_ptr

  1. ☑ Select option 1), then see:

For Other Fields

  1. ⚠ Select option 1) and hope for the best.
  2. ☑ Select option 2) and provide a sensible default (not None a.k.a. null).
  3. ⚠ (blog post) (hack) Add A Migration For A Non-Null Foreignkey Field In Django

Follow-Up Error

If you permit Null to be a default, then you may have this new error:

django.db.utils.IntegrityError: column "..." contains null values

Solutions

  1. delete relevant migration files and rebuild migrations
  2. delete all migration files and rebuild migrations

Notes

cmsplugin_ptr

If the field is cmsplugin_ptr then know that

W. Bomar learned everything in the intitial version of this document after trying to overwrite cmsplugin_ptr while extending its model from source a. His solution was delete all migration files and rebuild migrations.

Appendix