Stupid question time. I seem to be able to create an object for a Django model even though I omit a column that was defined as NOT NULL and I don't understand why. Here's my model:
class Movie(models.Model):name = models.CharField(max_length=256)length_minutes = models.IntegerField()rating = models.CharField(max_length=2)class Meta:db_table = 'movies'
When I run python manage.py
sql I see:
BEGIN;
CREATE TABLE "movies" ("id" serial NOT NULL PRIMARY KEY,"name" varchar(256) NOT NULL,"length_minutes" integer NOT NULL,"rating" varchar(2) NOT NULL
);
COMMIT;
Also, if I run the command \d movies
from the psql
, I can see that all columns are designated NOT NULL
.
But here's what I don't understand. When I run the Python shell, I can issue the following command and a new row with an empty 'name' column will be created:
Movie.objects.create(length_minutes=120, rating='PG')
However, if I issue (what I believe to be) the equivalent SQL command:
INSERT INTO movies(length_minutes, rating) VALUES(120, 'PG');
... I get the error I would expect: "ERROR: null value... violates not-null constraint."
Why does Django's ORM allow me to create an object that lacks a parameter for a NOT NULL
CharField column? Does it assume that I'm using model validators? If so, it seems to be a pretty dumb and trusting ORM.
I'm using Python 2.7.1, Django 1.4, and PostgreSQL 9.1.4.
Thanks for your help.