Here is some code pulled from my (using postgres) django application
that recovers fine. Perhaps this is helpful to you. I am storing the
non-unique values and dealing with them later (pulling from an external
source that is supposed to have unique IDs assigned but they don't
always pan out somehow...).
from django.db import IntegrityError, transaction
#Other Code
...
#relevant code
try:
sid = transaction.savepoint()
foo = FooModel(fooarg=fooarg, ...)
foo.save()
except IntegrityError:
transaction.savepoint_rollback(sid)
non_unique_foo.append((FooDB, FooID))
Hope this helps!
Casey
On 02/01/2011 11:48 AM, Xavier Ordoquy wrote:
Le 1 févr. 2011 à 15:59, bruno desthuilliers wrote :
On 1 fév, 15:24, Xavier Ordoquy<xordo...@linovia.com> wrote:
Hi all,
I got a project which sometime outputs database integrity errors (key violates
unique constraint).
It is fine since I'm keeping an events table which may have several creation
messages for the same object.
Hmmm... Either I misunderstood you or you have a design problem here.
You probably miss the big picture (ie, several legacy databases, each with
their own models plus synchronization between them and a central django
database).
But anyway:
Until now, I was just catching the exception and forgot about it.
Today, I'm not sure this is the right way.
If the exception in itself is correctly processed, I noticed that any database
operation behind raises another exception saying that postgresql hates me and
won't perform any other operation (current transation is aborted, commands
ignored until end of transaction block.).
I already tried to end the transaction, but django will let me know I'm not
within a transaction....
Hu ?
I assume you did read this:
http://docs.djangoproject.com/en/dev/topics/db/transactions
and specially this part:
http://docs.djangoproject.com/en/dev/topics/db/transactions/#handling-exceptions-within-postgresql-transactions
Actually, I posted because this just doesn't seem to work.
If I try to rollback the transaction, I just get a nice error that I'not within
a transaction.
I haven't tested savepoints, but transaction rollback and autocommit doesn't
seem to work here.
I'll probably write a sample project this week to point the issue.
Regards,
Xavier.
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.