Fixed #33187 -- Made inspectdb handle ForeignKey.to_field attribute.

This commit is contained in:
jordan.bae 2021-10-31 00:08:06 +09:00 committed by Mariusz Felisiak
parent dab48b7482
commit aaf9b55858
3 changed files with 22 additions and 2 deletions

View File

@ -116,13 +116,17 @@ class Command(BaseCommand):
extra_params['unique'] = True
if is_relation:
ref_db_column, ref_db_table = relations[column_name]
if extra_params.pop('unique', False) or extra_params.get('primary_key'):
rel_type = 'OneToOneField'
else:
rel_type = 'ForeignKey'
ref_pk_column = connection.introspection.get_primary_key_column(cursor, ref_db_table)
if ref_pk_column and ref_pk_column != ref_db_column:
extra_params['to_field'] = ref_db_column
rel_to = (
"self" if relations[column_name][1] == table_name
else table2model(relations[column_name][1])
'self' if ref_db_table == table_name
else table2model(ref_db_table)
)
if rel_to in known_models:
field_type = '%s(%s' % (rel_type, rel_to)

View File

@ -21,6 +21,12 @@ class PeopleMoreData(models.Model):
license = models.CharField(max_length=255)
class ForeignKeyToField(models.Model):
to_field_fk = models.ForeignKey(
PeopleMoreData, models.CASCADE, to_field='people_unique',
)
class DigitsInColumnName(models.Model):
all_digits = models.CharField(max_length=11, db_column='123')
leading_digit = models.CharField(max_length=11, db_column='4extra')

View File

@ -204,6 +204,16 @@ class InspectDBTestCase(TestCase):
output,
)
@skipUnlessDBFeature('can_introspect_foreign_keys')
def test_foreign_key_to_field(self):
out = StringIO()
call_command('inspectdb', 'inspectdb_foreignkeytofield', stdout=out)
self.assertIn(
"to_field_fk = models.ForeignKey('InspectdbPeoplemoredata', "
"models.DO_NOTHING, to_field='people_unique_id')",
out.getvalue(),
)
def test_digits_column_name_introspection(self):
"""Introspection of column names consist/start with digits (#16536/#17676)"""
char_field_type = connection.features.introspected_field_types['CharField']