﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
15117	"get_or_create() raises ""DatabaseError: no such savepoint"" instead of IntegrityError (PostgreSQL)"	Antti Kaihola	Aymeric Augustin	"Given the following models in a `world` app:

{{{
class Country(models.Model):
    name = models.CharField(max_length=50)

class CountryExtraInfo(models.Model):
    country = models.ForeignKey(Country, primary_key=True)
    info = models.TextField(blank=True, default='')
}}}

and assuming no Countries have been created yet, the following happens:

{{{
>>> from world.models import CountryExtraInfo
>>> CountryExtraInfo.objects.get_or_create(pk=1)
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""django/db/models/manager.py"", line 135, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File ""django/db/models/query.py"", line 389, in get_or_create
    transaction.savepoint_rollback(sid, using=self.db)
  File ""django/db/transaction.py"", line 235, in savepoint_rollback
    connection._savepoint_rollback(sid)
  File ""django/db/backends/__init__.py"", line 70, in _savepoint_rollback
    self.cursor().execute(self.ops.savepoint_rollback_sql(sid))
  File ""django/db/backends/postgresql_psycopg2/base.py"", line 44, in execute
    return self.cursor.execute(query, args)
DatabaseError: no such savepoint
}}}

Also:

{{{
>>> from world.models import Country, CountryExtraInfo
>>> c = Country()
>>> c.id = 1
>>> CountryExtraInfo.objects.get_or_create(country=c)
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""django/db/models/manager.py"", line 135, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File ""django/db/models/query.py"", line 389, in get_or_create
    transaction.savepoint_rollback(sid, using=self.db)
  File ""django/db/transaction.py"", line 235, in savepoint_rollback
    connection._savepoint_rollback(sid)
  File ""django/db/backends/__init__.py"", line 70, in _savepoint_rollback
    self.cursor().execute(self.ops.savepoint_rollback_sql(sid))
  File ""django/db/backends/postgresql_psycopg2/base.py"", line 44, in execute
    return self.cursor.execute(query, args)
DatabaseError: no such savepoint
}}}

In these situations, it would make more sense to get an !IntegrityError instead.

Test environment:
 * Python 2.6.6
 * Django r15242
 * PostgreSQL 8.4.6
 * psycopg2 2.2.1"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed		dougal85@… sirmmo@…	Accepted	0	0	0	0	0	0
