On 6/20/2012 11:46 AM, Melvyn Sopacua wrote:
> On 20-6-2012 20:31, André Pang wrote:
>> Right. There's currently no way to enforce required=True at the database
>> level.
>
> So, your model clearly defines that it doesn't allow empty values, yet
> some were inserted? Can you pull this out of the abstract with an example?
There's no way for a model to disallow empty values, AFAIK. The example
I have resembles this:
class Foo(models.Model):
my_field = models.CharField(null=False, blank=True)
In Oracle's case, the null=False (which is the default) is ignored and
always coerced to null=True, which means that empty strings are allowed.
(I'm not sure what the behavior is for other databases; I guess it
depends on whether the backend regards NULL == '' or not.)
Since blank=True is an admin thing only, and required=True is an
attribute of a form and not a model, any non-form or non-admin code that
creates a model can insert an empty string into the database. Most of
the code we've written so far isn't form code nor admin code, e.g. we
have command-line Django scripts that import records from a legacy
FileMaker Pro database to Oracle, with a lot of garbage data containing
empty strings in various columns where they should be illegal.
Enforcing this at the database level would be nice.
> I think a more worthwhile solution is to have the default value for a
> field be propagated to the database as default value. In that case,
> empty strings on NOT NULL columns will be consistent.
In our case, there is no sensible default value for the field; e.g. what
if the field's a ForeignKey or a primary key[1]? The schema we have
requires that the column have good data or not exist at all, which I
don't think is insensible.
1. In Oracle's case, what if you have a model with a primary key that's
a CharField? I suspect that Oracle backend will coerce that to be
null=True, so now you have a primary key field that could have NULL.