Can I perform an "alter table" to remove a "not null" constraint?

295 views
Skip to first unread message

Gchorn

unread,
May 31, 2011, 12:52:27 AM5/31/11
to Django users
It seems as though the default in Django when creating tables is to
place a constraint that doesn't allow null values (contrary to what
I've learned is the default for SQL in general). I have a model which
has a date attribute, and which I now want to be able to hold null
values, but when I tried doing this by adding 'null = True' and 'blank
= True' to the model's field options, Django generates a

model.date may not be NULL

error when I try to leave the field blank in the Django admin
interface. This suggests I need to change something about the
database, rather than the model, correct? If so, what is the SQL
syntax for doing this? My research online has only turned up how to
apply a "not-null" constraint on a database table when creating the
table, not how to remove a "not-null" constraint on a table after it's
been created. Thanks in advance for any help on this!

Kirill Spitsin

unread,
May 31, 2011, 4:39:44 AM5/31/11
to django...@googlegroups.com
On Mon, May 30, 2011 at 09:52:27PM -0700, Gchorn wrote:
> It seems as though the default in Django when creating tables is to
> place a constraint that doesn't allow null values (contrary to what
> I've learned is the default for SQL in general). I have a model which
> has a date attribute, and which I now want to be able to hold null
> values, but when I tried doing this by adding 'null = True' and 'blank
> = True' to the model's field options, Django generates a

Use South (http://south.aeracode.org/).

Or you can run ./manage.py dbshell, and ALTER TABLE yourself, for
example, for PostgreSQL::

ALTER TABLE <app_model> ALTER COLUMN <col> DROP NOT NULL;

--
Kirill Spitsin

Reply all
Reply to author
Forward
0 new messages