django.db.utils.IntegrityError: null value in column “layer_id_id” violates not-null constraint . How to debug ?

843 views
Skip to first unread message

Satyajit Sarangi

unread,
Jun 15, 2011, 5:25:24 PM6/15/11
to Django users
his is my models.py

from django.db import models

class Sdr_Layer(models.Model): layer_name =
models.CharField(max_length = 100)
layer_attribute_name=models.CharField(max_length=100)

# This is an auto-generated Django model module created by ogrinspect.
from django.contrib.gis.db import models








# This is an auto-generated Django model module created by ogrinspect.
from django.contrib.gis.db import models

class Test(models.Model):
layer_id= models.ForeignKey(Sdr_Layer)
name = models.CharField(max_length=80)
descriptio = models.CharField(max_length=80)
geom = models.PolygonField(srid=4326)
objects = models.GeoManager()
S = Sdr_Layer(layer_name="Test")

# Auto-generated `LayerMapping` dictionary for Test model
test_mapping = {
'name' : 'Name',
'descriptio' : 'Descriptio',
'geom' : 'POLYGON25D',
}
S.layer_attribute_name=test_mapping
S.save();

When I am trying to load data to Test model , it throws me a not_null
error on layer_id which is the foreign key here . I basically want the
primary key of Sdr_Layer to act as a foreign key here . Sdr_Layer
table does have values . So , why such an error ?

Daniel Roseman

unread,
Jun 16, 2011, 4:49:28 AM6/16/11
to django...@googlegroups.com
You need to think things through a bit more carefully. It seems like you're randomly assigning things without really having any idea of what you're doing.

This code:
S = Sdr_Layer(layer_name="Test") 
instantiates a new Sdr_Layer object, with the name "Test".

This line:
S.layer_attribute_name=test_mapping 
tries to do something very strange, which is to assign a dictionary to your CharField. You can do that, but it will be converted to a string on save.

Now, none of this code would throw a "not_null error on layer_id", because layer_id is a field on the Test model, which you don't even use in this code. So, there must be code you're not showing us. Also, there is a reason why Python prints the full traceback on an error: it's useful debugging information, not random cruft. Post it too.

FWIW, I suspect you wanted to do this:

t = Test(**layer_mapping)
t.layer_id = S
t.save()
--
DR.

Satyajit Sarangi

unread,
Jun 16, 2011, 4:53:30 AM6/16/11
to django...@googlegroups.com
What I want is a very simple thing . 
I want the primary key of Sdr_Layer to be the foreign key of Test table . How do I manage it ? 
And , I am assigning values to Test via Layermapping tool , which as you said that passes a dict . 

I think the problem here is the layermapping tries to map onto the Test model , but doesn't know what to do about the layer_id  . Thats what I think . Not sure . What can be the way out ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/mELn7nUrAOMJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Satyajit Sarangi

Reply all
Reply to author
Forward
0 new messages