On Wed, Dec 7, 2011 at 10:59 AM, Yo-Yo Ma <[email protected]> wrote:
> Take the following models:
>
> class Person(models.Model):
>    name = models.CharField(_('Person'), max_length=255)
>
> class Production(models.Model):
>    person = models.ForeignKey(Person)
>    title = models.CharField(_('Title'), max_length=255)
>
>    class Meta:
>        unique_together = (('person', 'title'),)
>
>
> Create a Person named "Bob", then create a view with an inline model
> formset (I used the factory function) for the Production. Then, using
> the inline forms in the Production formset, add a couple of Production
> objects, one with the title "One", and one with the title "Two". After
> you save them, edit the title fields of "One" and "Two" to change the
> values to "Two" and "One", respectively. Upon attempting to save,
> you'll receive an error message stating, "Production with this Person
> and Title already exists."

Hi,

Two things:

Firstly, procedural -- if you've fairly certainly you've found a bug,
there's no need to start a thread on django-developers; just open a
ticket. The only reason to start a thread is if you've found something
that you're not sure is a bug, or if you're looking to start a design
discussion over the right way to address a bug.

Secondly (and this would have been my comment on a ticket if had been
created), this strikes me as something that should only be a problem
if you're in transaction autocommit mode. Saving an inline formset
isn't an atomic action, so the situation you describe is effectively
the analog of the following Python:

x = y
y = x

Which, of course, won't give you the result you expect; the database
error is just protecting you from the mistake. What you need to say
is:

x, y = y, x

The database equivalent of this line is to use a transaction. I
haven't tested this, but I'd be surprised if wrapping the inline
formset save in a transaction didn't address this issue (as long as
you're using a database that has real transactions -- MySQL InnoDB
doesn't count, due to its... interesting... interpretation of
constraint validation).

As long as this works under a transaction, I don't consider it a bug
that Django exposes a database error in this case. The overhead
involved in providing a workaround would be significant, and it's only
required for a very specific subset of cases.

Yours,
Russ Magee %-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to