charField null=false not enforced for table creation in oracle

147 views
Skip to first unread message

Wayne

unread,
Feb 26, 2010, 5:35:55 PM2/26/10
to Django users
Hi,
I try to create tables using Django models in our test oracle
database. I define a model with one column like this: name =
models.CharField(max_length=512, null=False, blank=False). After
running python manage.py syncdb, I found that column 'name' in the
generated table still allows null value in the column definition. I
understand that null = false is the default option. And I verified
that for other types of Django fields, null = false or no such option
at all would generate columns that do not allow null values in the
database. Is there particular reason why null = false is not enforced
for charField?
My Django version is 1.1.1, Python version is 2.6, and oracle server
is 10g release 2.

Many thanks for your help.

Wayne

Karen Tracey

unread,
Feb 26, 2010, 8:47:50 PM2/26/10
to django...@googlegroups.com
On Fri, Feb 26, 2010 at 5:35 PM, Wayne <waynes...@gmail.com> wrote:
Hi,
I try to create tables using Django models in our test oracle
database. I define a model with one column like this: name =
models.CharField(max_length=512, null=False, blank=False). After
running python manage.py syncdb, I found that column 'name' in the
generated table still allows null value in the column definition. I
understand that null = false is the default option. And I verified
that for other types of Django fields, null = false or no such option
at all would generate columns that do not allow null values in the
database. Is there particular reason why null = false is not enforced
for charField?

Yes. Oracle does not distinguish between null and an empty string. So null=False applied at the database level would mean that you also could not store the empty string in that field, which would be quite different from what you would see running the same code on most other databases, where the empty string is a valid value for a NOT NULL character field.

The behavior is documented, see the note here: http://docs.djangoproject.com/en/dev/ref/models/fields/#null. The note is perhaps not as clear as it could be about when the empty string is allowed for a field. That's a property of the field type (for example, empty strings are allowed for CharField but not a DateField), and is not in any way tied to the value of the field's blank option. The blank option, as noted in its description, is used for validation and is not applied at the database level.

Karen

Wayne

unread,
Mar 11, 2010, 8:57:13 AM3/11/10
to Django users
Hi Karen,

Many thanks for your reply.
Now our business requirements do not allow either empty string or null
value for this particular charField column (name =
models.CharField(max_length=512, null=False, blank=False)). How do you
suggest we should configure this field so that we can enforce not null
condition in the oracle database by using syncdb? Currently, after
creating those tables we need to manually modify the column properties
in Oracle to make them not null. Or Django Oracle backend deliberately
ignores null=false for charField creation?

Wayne

On Feb 26, 8:47 pm, Karen Tracey <kmtra...@gmail.com> wrote:

Skylar Saveland

unread,
Feb 23, 2013, 1:27:59 AM2/23/13
to django...@googlegroups.com
This is still a good question :)

Ian

unread,
Feb 23, 2013, 2:35:00 PM2/23/13
to django...@googlegroups.com
On Friday, February 22, 2013 11:27:59 PM UTC-7, Skylar Saveland wrote:
This is still a good question :)

The answer is that there is no way to instruct Django that you really, truly want this CharField to be NOT NULL even on Oracle, and that is unlikely to change.  Part of the philosophy in the development of the Oracle backend was that Oracle DBAs would not likely be running syncdb directly, but that devs would be more likely to run sqlall and hand-edit the scripts as needed (e.g. to add storage clauses) before handing them off to the DBA.  As a result of this thinking, not every possible Oracle idiosyncrasy is directly addressable via model options, and we are reluctant to add general features that are only useful for Oracle.
Reply all
Reply to author
Forward
0 new messages