diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py
index 76073c7..76d2838 100644
a
|
b
|
class Command(NoArgsCommand):
|
62 | 62 | if ' ' in att_name or '-' in att_name or keyword.iskeyword(att_name) or column_name != att_name: |
63 | 63 | extra_params['db_column'] = column_name |
64 | 64 | |
| 65 | # Add primary_key and unique, if necessary. |
| 66 | if column_name in indexes: |
| 67 | if indexes[column_name]['primary_key']: |
| 68 | extra_params['primary_key'] = True |
| 69 | elif indexes[column_name]['unique']: |
| 70 | extra_params['unique'] = True |
| 71 | |
65 | 72 | # Modify the field name to make it Python-compatible. |
66 | 73 | if ' ' in att_name: |
67 | 74 | att_name = att_name.replace(' ', '_') |
… |
… |
class Command(NoArgsCommand):
|
88 | 95 | extra_params.update(field_params) |
89 | 96 | comment_notes.extend(field_notes) |
90 | 97 | |
91 | | # Add primary_key and unique, if necessary. |
92 | | if column_name in indexes: |
93 | | if indexes[column_name]['primary_key']: |
94 | | extra_params['primary_key'] = True |
95 | | elif indexes[column_name]['unique']: |
96 | | extra_params['unique'] = True |
97 | | |
98 | 98 | field_type += '(' |
99 | 99 | |
100 | 100 | if keyword.iskeyword(att_name): |
diff --git a/tests/regressiontests/inspectdb/models.py b/tests/regressiontests/inspectdb/models.py
index fc2548b..fac47aa 100644
a
|
b
|
class People(models.Model):
|
6 | 6 | |
7 | 7 | class Message(models.Model): |
8 | 8 | from_field = models.ForeignKey(People, db_column='from_id') |
| 9 | |
| 10 | class PeopleData(models.Model): |
| 11 | people_pk = models.ForeignKey(People, primary_key=True) |
| 12 | ssn = models.CharField(max_length=11) |
| 13 | |
| 14 | class PeopleMoreData(models.Model): |
| 15 | people_unique = models.ForeignKey(People, unique=True) |
| 16 | license = models.CharField(max_length=255) |
| 17 | |
diff --git a/tests/regressiontests/inspectdb/tests.py b/tests/regressiontests/inspectdb/tests.py
index 683e6e9..e2eced5 100644
a
|
b
|
class InspectDBTestCase(TestCase):
|
13 | 13 | error_message = "inspectdb generated an attribute name which is a python keyword" |
14 | 14 | self.assertNotIn("from = models.ForeignKey(InspectdbPeople)", out.getvalue(), msg=error_message) |
15 | 15 | self.assertIn("from_field = models.ForeignKey(InspectdbPeople)", out.getvalue()) |
| 16 | self.assertIn("people_pk = models.ForeignKey(InspectdbPeople, primary_key=True)", |
| 17 | out.getvalue()) |
| 18 | self.assertIn("people_unique = models.ForeignKey(InspectdbPeople, unique=True)", |
| 19 | out.getvalue()) |
16 | 20 | out.close() |