Hi, I have created a management command that populates some tables in
a Postgres database, and I use the handy get_or_create() method in db/
models/query.py. I tried running a command recently where I had
inadvertently left some foreign key references dangling around, but
instead of a useful error, I instead got the obscure error "no such
savepoint." The code catches the original exception that would have
helped me realize my own error, and instead raises a new exception
about "no such savepoint."
Here is the code with get_or_create() that obscures the error:
except IntegrityError, e:
transaction.savepoint_rollback(sid)
In order to see actual errors, I think I need to something with
management commands that allows the savepoint to be rolled back. Has
anybody encountered this before? I've skimmed some long threads
pertaining to Django/psycopg/Postgres interactions with respect to
setting up transaction management, but I have to admit that most of
the discussion has been over my head.
Thanks,
Steve
P.S. Here is the entire method for get_or_create(), for more context:
def get_or_create(self, **kwargs):
"""
Looks up an object with the given kwargs, creating one if
necessary.
Returns a tuple of (object, created), where created is a
boolean
specifying whether an object was created.
"""
assert kwargs, \
'get_or_create() must be passed at least one keyword
argument'
defaults = kwargs.pop('defaults', {})
try:
return self.get(**kwargs), False
except self.model.DoesNotExist:
try:
params = dict([(k, v) for k, v in kwargs.items() if
'__' not in k])
params.update(defaults)
obj = self.model(**params)
sid = transaction.savepoint()
obj.save(force_insert=True)
transaction.savepoint_commit(sid)
return obj, True
except IntegrityError, e:
transaction.savepoint_rollback(sid)
try:
return self.get(**kwargs), False
except self.model.DoesNotExist:
raise e
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---