Fixed #27096 -- Fixed primary key introspection for sqlite3 backend

This commit is contained in:
Claude Paroz 2016-08-20 10:28:42 +02:00
parent 9f27735612
commit 00bb47b58f
3 changed files with 5 additions and 3 deletions

View File

@ -214,7 +214,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
results = results[results.index('(') + 1:results.rindex(')')]
for field_desc in results.split(','):
field_desc = field_desc.strip()
m = re.search('"(.*)".*PRIMARY KEY( AUTOINCREMENT)?$', field_desc)
m = re.search('"(.*)".*PRIMARY KEY( AUTOINCREMENT)?', field_desc)
if m:
return m.groups()[0]
return None

View File

@ -15,7 +15,7 @@ class City(models.Model):
@python_2_unicode_compatible
class District(models.Model):
city = models.ForeignKey(City, models.CASCADE)
city = models.ForeignKey(City, models.CASCADE, primary_key=True)
name = models.CharField(max_length=50)
def __str__(self):

View File

@ -6,7 +6,7 @@ from django.db import connection
from django.db.utils import DatabaseError
from django.test import TransactionTestCase, mock, skipUnlessDBFeature
from .models import Article, ArticleReporter, City, Reporter
from .models import Article, ArticleReporter, City, District, Reporter
class IntrospectionTests(TransactionTestCase):
@ -165,7 +165,9 @@ class IntrospectionTests(TransactionTestCase):
def test_get_primary_key_column(self):
with connection.cursor() as cursor:
primary_key_column = connection.introspection.get_primary_key_column(cursor, Article._meta.db_table)
pk_fk_column = connection.introspection.get_primary_key_column(cursor, District._meta.db_table)
self.assertEqual(primary_key_column, 'id')
self.assertEqual(pk_fk_column, 'city_id')
def test_get_indexes(self):
with connection.cursor() as cursor: