100👍
You must create a migration, where you will specify default value for a new field, since you don’t want it to be null. If null is not required, simply add null=True
and create and run migration.
16👍
coldmind‘s answer is correct but lacks details.
The NOT NULL constraint failed
occurs when something tries to set None
to the zipcode
property, while it has not been explicitly allowed.
It usually happens when:
-
Your field has
Null=False
by default, so that the value in the database cannot beNone
(i.e. undefined) when the object is created and saved in the database (this happens after aobjects_set.create()
call or setting the.zipcode
property and doing a.save()
call).For instance, if somewhere in your code an assignment results in:
model.zipcode = None
This error is raised.
-
When creating or updating the database, Django is constrained to find a default value to fill the field, because
Null=False
by default. It does not find any because you haven’t defined any. So this error can not only happen during code execution but also when creating the database? -
Note that the same error would be returned if you define
default=None
, or if your default value with an incorrect type, for instancedefault='00000'
instead of00000
for your field (maybe can there be an automatic conversion between char and integers, but I would advise against relying on it. Besides, explicit is better than implicit). Most likely an error would also be raised if the default value violates the max_length property, e.g. 123456So you’ll have to define the field by one of the following:
models.IntegerField(_('zipcode'), max_length=5, Null=True, blank=True) models.IntegerField(_('zipcode'), max_length=5, Null=False, blank=True, default=00000) models.IntegerField(_('zipcode'), max_length=5, blank=True, default=00000)
and then make a migration (
python3 manage.py makemigration <app_name>
) and then migrate (python3 manage.py migrate
).For safety you can also delete the last failed migration files in
<app_name>/migrations/
, there are usually named after this pattern:<NUMBER>_auto_<DATE>_<HOUR>.py
Finally, if you don’t set Null=True
, make sure that mode.zipcode = None
is never done anywhere.
- [Django]-How to escape {{ or }} in django template?
- [Django]-Class Based Views VS Function Based Views
- [Django]-Could not find a version that satisfies the requirement pkg-resources==0.0.0
8👍
If the zipcode
field is not a required field then add null=True
and blank=True
, then run makemigrations and migrate command to successfully reflect the changes in the database.
- [Django]-Celery: WorkerLostError: Worker exited prematurely: signal 9 (SIGKILL)
- [Django]-Auto-reloading of code changes with Django development in Docker with Gunicorn
- [Django]-Matching query does not exist Error in Django
0👍
In Django Null=True
means Null Values are accepted. But Some Filed having Django that Blank=True
are not satisfied for put Blank fields.
- DateTimeField
- ForeignKey
These two fields are used and if you want to put Blank I recommend adding NULL=TRUE
- [Django]-Assign variables to child template in {% include %} tag Django
- [Django]-Django database query: How to get object by id?
- [Django]-Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?
-6👍
Since you added a new property to the model, you must first delete the database. Then manage.py migrations then manage.py migrate.
- [Django]-Django form – set label
- [Django]-Annotate with latest related object in Django
- [Django]-Django: Record with max element