Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » django breaks not null in postgresql? [RSS Feed]

#1 Nov. 26, 2005 10:40:22

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


hi,
i have a table
name varchar not null
title varchar not null
blurb varchar not null

in django, if i want to insert a new record and just give a value
for the 'name' field and no values for the rest - it gets saved.
Postgresql should barf saying 'lack of value in xxx field violates
not null constraint' but it doesnt. This looks very much like a
serious bug to me
--
regards
kghttp://www.livejournal.com/users/lawgontally ho!http://avsap.org.inಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

Offline

#2 Nov. 26, 2005 18:27:19

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


On 11/26/05, Kenneth Gonsalves <> wrote:
> i have a table
> name varchar not null
> title varchar not null
> blurb varchar not null
>
> in django, if i want to insert a new record and just give a value
> for the 'name' field and no values for the rest - it gets saved.
> Postgresql should barf saying 'lack of value in xxx field violates
> not null constraint' but it doesnt. This looks very much like a
> serious bug to me

Django does indeed put in the "NOT NULL"s when outputting its CREATE
TABLE statements. What's happening in your case is that the title and
blurb fields are getting their default value, which is "" (the empty
string).

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#3 Nov. 27, 2005 02:24:52

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


On Saturday 26 Nov 2005 11:57 pm, Adrian Holovaty wrote:
> On 11/26/05, Kenneth Gonsalves <> wrote:
> > i have a table
> > name varchar not null
> > title varchar not null
> > blurb varchar not null
> >
> > in django, if i want to insert a new record and just give a
> > value for the 'name' field and no values for the rest - it gets
> > saved. Postgresql should barf saying 'lack of value in xxx
> > field violates not null constraint' but it doesnt. This looks
> > very much like a serious bug to me
>
> Django does indeed put in the "NOT NULL"s when outputting its
> CREATE TABLE statements. What's happening in your case is that
> the title and blurb fields are getting their default value, which
> is "" (the empty string).

so how do you prevent that? i dont like to waste code checking if
all the mandatory fields are filled - i want postgres to do that.
Looking at the table, there is no mention of a default value.
>
> Adrian
>
> --
> Adrian Holovaty
> holovaty.com | djangoproject.com | chicagocrime.org

--
regards
kghttp://www.livejournal.com/users/lawgontally ho!http://avsap.org.inಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

Offline

#4 Nov. 30, 2005 14:44:11

Nebojša Đ.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


On 27-11-2005, at 3:21, Kenneth Gonsalves wrote:so how do you prevent that? i dont like to waste code checking if
all the mandatory fields are filled - i want postgres to do that.
Looking at the table, there is no mention of a default value.Can you post your model? It seems to me that you have something likethis:name = meta.CharField(maxlength=200, blank=True)

If you want to check for empty data you can just use:

name = meta.CharField(maxlength=200)

and django itself will check that name field is not *blank* or *NULL*.

---
Nebojša Đorđević - nesh
Studio Quattro - Niš - SCGhttp://djnesh.blogspot.com/|http://djnesh-django.blogspot.com/Registered Linux User 282159

Offline

#5 Dec. 1, 2005 01:48:02

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


On Wednesday 30 Nov 2005 8:13 pm, Nebojša Đorđević - nesh wrote:
> On 27-11-2005, at 3:21, Kenneth Gonsalves wrote:
> > so how do you prevent that? i dont like to waste code checking
> > if all the mandatory fields are filled - i want postgres to do
> > that. Looking at the table, there is no mention of a default
> > value.
>
> Can you post your model? It seems to me that you have something
> like this:
>
> name = meta.CharField(maxlength=200, blank=True)

no
>
> If you want to check for empty data you can just use:
>
> name = meta.CharField(maxlength=200)

i have this
>
> and django itself will check that name field is not *blank* or
> *NULL*.

django admin interface checks - but if i do it from a form on the
main site, i have to check for empty fields, otherwise save()
simply inserts blanks (not nulls) in all fields not filled up. So
whats the point of having the 'not null' constraint in my table?
--
regards
kghttp://www.livejournal.com/users/lawgontally ho!http://avsap.org.inಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

Offline

#6 Dec. 1, 2005 01:55:33

Joshua D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


Kenneth Gonsalves wrote:hi,
i have a table
name varchar not null
title varchar not null
blurb varchar not nullin django, if i want to insert a new record and just give a valuefor the 'name' field and no values for the rest - it gets saved.Postgresql should barf saying 'lack of value in xxx field violatesnot null constraint' but it doesnt. This looks very much like aserious bug to meIt should be noted that NULL != ''. If you say not null for a text fieldyou can stillenter a '' (blank string). That is perfectly valid. If you don't wantnull or blank youcan say blank=False and null=False will give you the desired behavior.

Joshua D. Drake




--
The PostgreSQL Company - Command Prompt, Inc. 1.503.667.4564
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Managed Services, Shared and Dedicated Hosting
Co-Authors: PLphp, PLperl -http://www.commandprompt.com/

Offline

#7 Dec. 1, 2005 05:51:45

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


On Thursday 01 Dec 2005 7:25 am, Joshua D. Drake wrote:
> >in django, if i want to insert a new record and just give a
> > value for the 'name' field and no values for the rest - it gets
> > saved. Postgresql should barf saying 'lack of value in xxx
> > field violates not null constraint' but it doesnt. This looks
> > very much like a serious bug to me
> >  
>
> It should be noted that NULL != ''. If you say not null for a
> text field you can still
> enter a '' (blank string). That is perfectly valid. If you don't
> want null or blank you
> can say blank=False and null=False will give you the desired
> behavior.

this is my model:

class Visitor(meta.Model):
title = meta.CharField('Title',maxlength=250)
name = meta.CharField('Name',maxlength=100)
email = meta.EmailField('Email Id')
date = meta.DateField(auto_now_add=True)
matter = meta.TextField('Matter')
approved = meta.BooleanField('Approved',default='true')

i type this at the python prompt:

p = visitors.Visitor(title=l'title')
p.save()

it saves, pg doesnt barf, and i get '' values in all the text
fields. I feel the behaviour should be a barf from pg.


--
regards
kghttp://www.livejournal.com/users/lawgontally ho!http://avsap.org.inಇಂಡ್ಲಿನಕ್ಸ வாழ்க!

Offline

#8 Dec. 1, 2005 06:20:36

Joshua D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


this is my model:

class Visitor(meta.Model):
title = meta.CharField('Title',maxlength=250)
name = meta.CharField('Name',maxlength=100)
email = meta.EmailField('Email Id')
date = meta.DateField(auto_now_add=True)
matter = meta.TextField('Matter')
approved = meta.BooleanField('Approved',default='true')

i type this at the python prompt:

p = visitors.Visitor(title=l'title')
p.save()it saves, pg doesnt barf, and i get '' values in all the textfields. I feel the behaviour should be a barf from pg.That feeling would be incorrect though :). Which is why I made mention of'' != NULL. It is an important distinction. A '' is a string, a blankstring butstill a string. A NULL is nothing, it does not exist, and has no value.

As a counter example, an integer may be NULL but may not be '' .

In order for PostgreSQL to not allow a blank or null in the colum you would
have to have something like this:CREATE TABLE foo (id bigserial primary key, name text check (name IS NOTNULL OR name !=''))I believe the model would look like this:

class Visitor(meta.Model):
name = meta.CharField('Name', maxlength=100,blank=False, null=False)

Here is a good explanation of NULL:http://www.metrokc.gov/gis/kb/Content/SQLTipNull.htmSincerely,
Joshua D. Drake






--
The PostgreSQL Company - Command Prompt, Inc. 1.503.667.4564
PostgreSQL Replication, Consulting, Custom Development, 24x7 support
Managed Services, Shared and Dedicated Hosting
Co-Authors: PLphp, PLperl -http://www.commandprompt.com/

Offline

#9 Dec. 1, 2005 08:52:36

Nebojša Đ.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

django breaks not null in postgresql?


On 1-12-2005, at 6:52, Kenneth Gonsalves wrote:title = meta.CharField('Title',maxlength=250)Fromhttp://www.djangoproject.com/documentation/model_api/#general-field-options:nullIf True, Django will store empty values as NULL in the database.Default is False.Note that empty string values will always get stored as emptystrings, not as NULL -- so use null=True for non-string fields suchas integers, booleans and dates.Avoid using null on string-based fields such as CharField andTextField unless you have an excellent reason. If a string-basedfield has null=True, that means it has two possible values for "nodata": NULL, and the empty string. In most cases, it's redundant tohave two possible values for "no data;" Django convention is to usethe empty string, not NULL.blank

If True, the field is allowed to be blank.Note that this is different than null. null is purely database-related, whereas blank is validation-related. If a field hasblank=True, validation on Django's admin site will allow entry of anempty value. If a field has blank=False, the field will be required.p = visitors.Visitor(title=l'title')
p.save()

it saves, pg doesnt barf, and i get '' values in all the text
fields. I feel the behaviour should be a barf from pg.Strange, IIRC this type of error is presumed to be catch by modelvalidators before data gets sent to database, or I'm guessing itwrong? It is not question why database is not sending a error, butwhy django model validators allow sending this to database.btw.approved = meta.BooleanField('Approved',default='true')it should be:
approved = meta.BooleanField(default=True)always use python native types when working with models - it will beconverted to database specific automatically. Also, if you don't useany of i18n stuff you don't need to specify verbose_name if is sameas a field name.---
Nebojša Đorđević - nesh
Studio Quattro - Niš - SCGhttp://djnesh.blogspot.com/|http://djnesh-django.blogspot.com/Registered Linux User 282159

Offline

  • Root
  • » Django
  • » django breaks not null in postgresql? [RSS Feed]

Board footer

Moderator control

Enjoy the 20th of January
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support