Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null value in column "name" violates not-null constraint when adding new item in django admin

I have added a new model to my admin. This is my models.py:

class EngineeringToolAttributeType(models.Model):
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=255, blank=True, null=True)
    api_url = models.CharField(max_length=255, blank=True, null=True)
    api_field = models.CharField(max_length=50, blank=True, null=True)
    active = models.BooleanField(default=True)

    def __str__(self):
        return self.name

And the admin.py:

from extras.models import EngineeringToolAttributeType
from django.contrib import admin

class EngineeringToolAttributeTypeAdmin(admin.ModelAdmin):
    fields = ['name', 'description', 'api_url', 'api_field', 'active']
    list_display = ('name', 'description', 'api_url', 'api_field', 'active')

admin.site.register(EngineeringToolAttributeType, EngineeringToolAttributeTypeAdmin)

When I try to add (click on add button via the admin), I get this error:

Internal Server Error: /website/admin/extras/engineeringtoolattributetype/add/

IntegrityError at /admin/extras/engineeringtoolattributetype/add/
null value in column "name" violates not-null constraint

This has never happened before. I know name is not allowed to be Null, but I'm still adding a record. How is that possible?

like image 944
Ruben Avatar asked Jun 27 '17 15:06

Ruben


People also ask

How do I add a NOT NULL constraint on a column containing null values?

It is possible to add a NOT NULL constraint to an existing table by using the ALTER TABLE statement. In this case, the column_name must not contain any NULL value before applying the NOT NULL constraint.

How do I allow null values in Django?

null=True will make the field accept NULL values. Blank values for Django field types such as DateTimeField or ForeignKey will be stored as NULL in the database.

How do I insert a non null field in an existing table?

You can add a not null column at the time of table creation or you can use it for an existing table. In the above table, we have declared Id as int type that does not take NULL value. If you insert NULL value, you will get an error. Here is the query to add a not null column in an existing table using alter command.

How do I deal with NOT NULL in PostgreSQL?

In PostgreSQL, we can add the NOT NULL Constraint to a column of an existing table with the ALTER TABLE command's help. The below illustrations of the ALTER TABLE command are used to add a NOT NULL constraint into the existing table: ALTER TABLE table_name. ALTER COLUMN column_name.


1 Answers

This issue is partially result of not using CharField and TextField properly. You should almost never use null=True on this fields and their subclasses. https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.Field.null

Avoid using null on string-based fields such as CharField and TextField. If a string-based field has null=True, that means it has two possible values for “no data”: NULL, and the empty string. In most cases, it’s redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL. One exception is when a CharField has both unique=True and blank=True set. In this situation, null=True is required to avoid unique constraint violations when saving multiple objects with blank values.

I would highly suggest removing this param from yourCharFields and TextFields.

Also just to be sure run ./manage.py makemigrations && ./manage.py migrate.

like image 197
Sardorbek Imomaliev Avatar answered Sep 28 '22 05:09

Sardorbek Imomaliev