diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
a
|
b
|
|
150 | 150 | cursor.execute("SET client_encoding to 'UNICODE'") |
151 | 151 | return UnicodeCursorWrapper(cursor, 'utf-8') |
152 | 152 | |
| 153 | def _commit(self): |
| 154 | if self.connection is not None: |
| 155 | try: |
| 156 | return self.connection.commit() |
| 157 | except Database.IntegrityError, e: |
| 158 | raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] |
| 159 | |
153 | 160 | def typecast_string(s): |
154 | 161 | """ |
155 | 162 | Cast all returned strings to unicode strings. |
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
a
|
b
|
|
189 | 189 | finally: |
190 | 190 | self.isolation_level = level |
191 | 191 | self.features.uses_savepoints = bool(level) |
| 192 | |
| 193 | def _commit(self): |
| 194 | if self.connection is not None: |
| 195 | try: |
| 196 | return self.connection.commit() |
| 197 | except Database.IntegrityError, e: |
| 198 | raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] |
diff --git a/tests/regressiontests/backends/models.py b/tests/regressiontests/backends/models.py
a
|
b
|
|
54 | 54 | class Meta: |
55 | 55 | db_table = 'CaseSensitive_Post' |
56 | 56 | |
| 57 | |
| 58 | class Reporter(models.Model): |
| 59 | first_name = models.CharField(max_length=30) |
| 60 | last_name = models.CharField(max_length=30) |
| 61 | |
| 62 | def __unicode__(self): |
| 63 | return u"%s %s" % (self.first_name, self.last_name) |
| 64 | |
| 65 | |
| 66 | class Article(models.Model): |
| 67 | headline = models.CharField(max_length=100) |
| 68 | pub_date = models.DateField() |
| 69 | reporter = models.ForeignKey(Reporter) |
| 70 | |
| 71 | def __unicode__(self): |
| 72 | return self.headline |
| 73 | |
| 74 | |
57 | 75 | qn = connection.ops.quote_name |
58 | 76 | |
59 | 77 | __test__ = {'API_TESTS': """ |
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
a
|
b
|
|
4 | 4 | import unittest |
5 | 5 | |
6 | 6 | from django.conf import settings |
7 | | from django.core import management |
8 | 7 | from django.core.management.color import no_style |
9 | | from django.db import backend, connection, connections, DEFAULT_DB_ALIAS |
| 8 | from django.db import backend, connection, connections, DEFAULT_DB_ALIAS, utils |
10 | 9 | from django.db.backends.signals import connection_created |
11 | 10 | from django.db.backends.postgresql import version as pg_version |
12 | | from django.test import TestCase |
| 11 | from django.test import TestCase, TransactionTestCase |
13 | 12 | |
14 | 13 | from regressiontests.backends import models |
15 | 14 | |
… |
… |
|
186 | 185 | data.clear() |
187 | 186 | cursor = connection.cursor() |
188 | 187 | self.assertTrue(data == {}) |
| 188 | |
| 189 | |
| 190 | class FkConstraintsTest(TransactionTestCase): |
| 191 | |
| 192 | def setUp(self): |
| 193 | # Create a Reporter. |
| 194 | self.r = models.Reporter.objects.create(first_name='John', last_name='Smith') |
| 195 | |
| 196 | def test_integrity_checks_on_creation(self): |
| 197 | """Try to create a model instance that violates a FK constraint. Should fail""" |
| 198 | a = models.Article(headline="This is a test", pub_date=datetime.datetime(2005, 7, 27), reporter_id=30) |
| 199 | try: |
| 200 | a.save() |
| 201 | except Exception, e: |
| 202 | self.assertEqual(utils.IntegrityError, type(e)) |
| 203 | |
| 204 | def test_integrity_checks_on_update(self): |
| 205 | """Try to update a model instance introducing a FK constraint violation. Should fail""" |
| 206 | # Create an Article. |
| 207 | models.Article.objects.create(headline="Test article", pub_date=datetime.datetime(2010, 9, 4), reporter=self.r) |
| 208 | # Retrive it from the DB |
| 209 | a = models.Article.objects.get(headline="Test article") |
| 210 | a.reporter_id = 30 |
| 211 | try: |
| 212 | a.save() |
| 213 | except Exception, e: |
| 214 | self.assertEqual(utils.IntegrityError, type(e)) |