getting "duplicate key violates unique constraint" error from form even with "unique_together" in model

1,878 views
Skip to first unread message

hotani

unread,
Jan 2, 2009, 12:57:33 PM1/2/09
to Django users
I have 3 fields that need to be unique together: office, county and
case_no. In the "Case" model, I have the following:

--
class Case(models.Model):
office = models.ForeignKey(Office, editable=False)
county = models.ForeignKey(County)
case_no = models.CharField(max_length=20)
...

class Meta:
unique_together = ("office", "county", "case_no")

--

Using forms, when I try to submit a duplicate I get a nasty 500 error:
"duplicate key violates unique constraint..." rather than a form
error.

This is a new constraint I added to the database and am trying to get
django to recognize it.

Karen Tracey

unread,
Jan 2, 2009, 1:17:31 PM1/2/09
to django...@googlegroups.com

Given what you have specified, Django form validation will be checking to ensure that the combo of office, county, and case_no is unique in the database.  Since you aren't getting a form error, apparently this unique check is passing.  You are instead getting a database level error (uncaught leading to the 500 error), meaning mostly lieley that the constraint you have placed in the DB differs somehow from what you have specified to Django.  What exactly does the database error say?  You left out the most important part for determining how what Django is checking differs from what the database is enforcing. 

If it seems they are the same, then ensure you are running 1.0.2 (there were some fixes made to unique checking between 1.0 and 1.0.2).  If that doesn't fix it, possibly you have hit a bug, but exact details of what you are getting from the DB would really help in tracking down what is going wrong.

Karen

hotani

unread,
Jan 2, 2009, 2:22:25 PM1/2/09
to Django users
This is from the 500 error:
--
IntegrityError at /case_edit/2651074/

duplicate key violates unique constraint
"case_no_office_county_unique"
--

I'm using the latest SVN build (9692).
This is what I used to create the constraint (since this was added
long after the table was created from django):
--
ALTER TABLE "main_case" ADD CONSTRAINT "case_no_office_county_unique"
UNIQUE ( "office_id", "county_id", "case_no" );
--

hotani

unread,
Jan 2, 2009, 2:45:23 PM1/2/09
to Django users
Could this be a problem with the editable=False setting?

The record I am attempting to duplicate is a duplicate office/county/
case_no entry. However, it is also a duplicate county/case_no. It
seems like even with the form validation ignoring 'office' it would
catch the county/case_no dupe?

I tried changing the Meta class to unique_together=
("county","case_no") but no change.

Karen Tracey

unread,
Jan 3, 2009, 11:05:31 AM1/3/09
to django...@googlegroups.com
On Fri, Jan 2, 2009 at 2:45 PM, hotani <hot...@gmail.com> wrote:

Could this be a problem with the editable=False setting?

Yes, sorry, I did not notice you had set that on one of the fields involved in the unique check.


The record I am attempting to duplicate is a duplicate office/county/
case_no entry. However, it is also a duplicate county/case_no. It
seems like even with the form validation ignoring 'office' it would
catch the county/case_no dupe?

No, if any of the fields involved in unique_together are not present in cleaned_data during form validation (the editable=False one is not) then the check cannot be performed.  county/case_no may be a dupe but without a value to check for office the form validation cannot determine if the three together are going to be unique. 


I tried changing the Meta class to unique_together=
("county","case_no") but no change.

I don't understand this.  When I try your model code and only specify these two in unique_together I get a form-level validation ('Case with this County and Case no already exists.') error trying to specify a duplicate county/case_no.

Karen

hotani

unread,
Jan 3, 2009, 6:34:28 PM1/3/09
to Django users
Thanks Karen, that fixes the problem in the django admin. At least now
I know that everything is ok on the database and model fronts.
However, in my own form it is still not validating this relationship.

Here is what I have right now:

in a forms.py file there is a base form for the Case model:
--
class CaseFormBase(forms.Form):
office = forms.ModelChoiceField(widget=forms.HiddenInput,
queryset=Office.objects.all())
case_no = forms.CharField(max_length=20, label="Case No.")
...
class Meta:
model=Case
--

This base form is used for other forms which need to dynamically build
their option lists. This is the form for adding a new case (in a file
called 'cases.py'):
--
from app.forms import CaseFormBase
class NewCaseForm(CaseFormBase):
county = forms.ModelChoiceField(queryset=County.objects.filter
(activecounties__office=office, activecounties__active=True ))
...

# form is validated and processed here:
if request.method=='POST':
f = NewCaseForm(request.POST)
if f.is_valid():
new_case = (...)
new_case.save()
--

Since this does not seem to be working, for now I've gone back to my
clean method which manually checks for duplicates. I'd rather do it
the 'right' way by using 'unique_together' but it doesn't seem to be
cooperating with my setup.

hotani

unread,
Jan 3, 2009, 7:26:15 PM1/3/09
to Django users
well, doy. I just noticed that when attempting to create a form from a
model I called forms.Form instead of ModelForm. I'll mess with it some
more and see what happens.
Reply all
Reply to author
Forward
0 new messages