Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django model field with default value set violates not null constraint when saved

My field:

signup_date = models.DateTimeField(blank=True,default=datetime.now)

My error when saving:

IntegrityError: null value in column "signup_date" violates not-null constraint

I'm trying to make a simple unit test where I create a bound instance of a ModelForm from a dict and save it.

Thanks.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/django/signupform/signup/insert_test_data.py", line 27, in <module>
    SDF.save()
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/forms/models.py", line 371, in save
    fail_message, commit, construct=False)
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/forms/models.py", line 86, in save_instance
    instance.save()
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/base.py", line 435, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/base.py", line 528, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/query.py", line 1479, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/sql/compiler.py", line 783, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/sql/compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/backends/util.py", line 15, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
IntegrityError: null value in column "signup_date" violates not-null constraint




from django.db import models
from django.contrib.localflavor.us.models import PhoneNumberField
from datetime import datetime
from models_dropdowns import *

class SignupData(models.Model):
    first_name = models.CharField(max_length=128,verbose_name='First Name')
    last_name = models.CharField(max_length=128,verbose_name='Last Name')
    street1 = models.CharField(max_length=128,verbose_name='Street Address 1')
    street2 = models.CharField(max_length=128,verbose_name='Street Address 2')
    city = models.CharField(max_length=128)
    state = models.CharField(max_length=2)
    zip = models.IntegerField(verbose_name='Zip Code')
    phone_number = PhoneNumberField(verbose_name='Phone Number XXX-XXX-XXXX')
    email = models.EmailField(verbose_name='Email Address')
    contact_method = models.ForeignKey('ContactMethodChoice',blank=False,default=-1,verbose_name='Preferred contact method')
    birth_date = models.DateField(verbose_name='Birth Date')
    policy_number = models.CharField(max_length=128,verbose_name='American Family Auto Insurance Policy Number') 
    vin = models.CharField(max_length=128,verbose_name='Vehicle Identification Number (VIN)')
    vehicle_make = models.ForeignKey('VehicleMakeChoice',verbose_name='VehicleMake')
    vehicle_model = models.CharField(max_length=128,verbose_name='Vehicle Model')
    vehicle_year = models.ForeignKey('VehicleYearChoice',verbose_name='Vehicle Year')
    vehicle_ownership = models.ForeignKey('VehicleOwnershipChoice',blank=False,default=-1,verbose_name='Vehicle Ownership')
    vehicle_use = models.ForeignKey('VehicleUseChoice',blank=False,default=-1,verbose_name='Use of Vehicle')
    terms_and_conditions = models.BooleanField(verbose_name='I Agree to the terms and conditions. (add link)')
    form_user_role = models.ForeignKey('FormUserRoleChoice',blank=False,default=-1,verbose_name='Your Role')
    participate_in_feedback = models.BooleanField(verbose_name='<b>Opportunity to provide feedback.</b>...<br><br>',help_text='Please check the box if you would like to participate.')
    signup_date = models.DateTimeField(blank=True,default=datetime.now,verbose_name='')

I use the following code to create a bound instance of the form and save it. I run this by importing it from ./manage.py shell.

from signupform.signup.forms import SignupDataForm

keys =      ('first_name','last_name','street1','street2','city','state','zip','phone_number','email','contact_method','birth_date','policy_number','vin','vehicle_make','vehicle_model','vehicle_year','vehicle_ownership','vehicle_use','terms_and_conditions','form_user_role','participate_in_feedback')

data = [
('firstname1','lastname1','test1','test1','test1','XX',55555,'555-555-5555','[email protected]',1,'01/01/01','####-####-##-##-AAAA-AA','123456789abcdefgh',1,'model',1996,1,1,True,1,True),
('firstname2','lastname2','test2','test2','test2','XX',55555,'555-555-5555','[email protected]',1,'01/01/01','####-####-##-##-AAAA-AA','123456789abcdefgh',1,'model',1996,1,1,True,1,True),
('firstname3','lastname3','test3','test3','test3','XX',55555,'555-555-5555','[email protected]',1,'01/01/01','####-####-##-##-AAAA-AA','123456789abcdefgh',1,'model',1996,1,1,True,1,True),
('firstname4','lastname4','test4','test4','test4','XX',55555,'555-555-5555','[email protected]',1,'01/01/01','####-####-##-##-AAAA-AA','123456789abcdefgh',1,'model',1996,1,1,True,1,True),
]

for d in data:
    tmpDict = {}
    for i in range(0,len(keys)):
        tmpDict[keys[i]] = d[i]
    SDF = SignupDataForm(tmpDict)
    if not SDF.is_valid():
        print SDF.errors
    else:
        SDF.save()
like image 972
Kevin Avatar asked Sep 02 '10 04:09

Kevin


1 Answers

On problem in you code for me is that you should not write

default=datetime.now()

but instead

default = datetime.now

passing the function as default and not the result of the function at the time the code is parsed. Currently, all your SignupData will have the same signup_date

That said, I am not sure that this is the cause of your bug.

like image 83
madewulf Avatar answered Nov 12 '22 01:11

madewulf