Skip to content

Commit 542fbfe

Browse files
authored
Merge pull request #139 from jazzband/pr/BlueMagma2/118
[#118] Fix: fix constraint creation using the wrong table and column name
2 parents 1bd2d44 + b877d9f commit 542fbfe

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Bug Fixes:
2424

2525
* #134 inspectdb should suppress output 'id = AutoField(primary_key=True)'
2626
* #134 fix for decreasing size of column with default by create-copy-drop-rename strategy.
27+
* #118 fix constraint creation using the wrong table and column name. Thanks to BlueMagma.
2728

2829
3.0.0 (2022/02/27)
2930
------------------

django_redshift_backend/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ def create_model(self, model):
270270
params.extend(extra_params)
271271
# FK
272272
if field.remote_field and field.db_constraint:
273-
to_table = field.remote_field.related_model._meta.db_table
274-
to_column = field.remote_field.related_model._meta.get_field(
273+
to_table = field.remote_field.model._meta.db_table
274+
to_column = field.remote_field.model._meta.get_field(
275275
field.remote_field.field_name
276276
).column
277277
if self.connection.features.supports_foreign_keys:

tests/test_migrations.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,61 @@ def test_alter_type_binary_to_char(self):
504504
'''ALTER TABLE test_pony DROP COLUMN "hash" CASCADE;''',
505505
'''ALTER TABLE test_pony RENAME COLUMN "hash_tmp" TO "hash";''',
506506
], sqls)
507+
508+
@postgres_fixture()
509+
def test_foreign_key_to_id(self):
510+
new_state = self.set_up_test_model('test')
511+
operations = [
512+
migrations.CreateModel(
513+
'Rider',
514+
[
515+
('id', models.AutoField(primary_key=True)),
516+
('pony', models.ForeignKey('Pony', models.CASCADE)),
517+
],
518+
),
519+
]
520+
with self.collect_sql() as sqls:
521+
self.apply_operations('test', new_state, operations)
522+
523+
if TEST_WITH_POSTGRES:
524+
identity = "serial"
525+
elif TEST_WITH_REDSHIFT:
526+
identity = "integer identity(1, 1)"
527+
528+
self.assertEqual([
529+
f'''CREATE TABLE "test_rider" ("id" {identity} NOT NULL PRIMARY KEY, "pony_id" integer NOT NULL) ;''',
530+
'''ALTER TABLE "test_rider" ADD CONSTRAINT "test_rider_pony_id_3c028c84_fk_test_pony_id"'''
531+
''' FOREIGN KEY ("pony_id") REFERENCES "test_pony" ("id");''',
532+
], sqls)
533+
534+
@postgres_fixture()
535+
def test_foreign_key_to_non_id(self):
536+
new_state = self.set_up_test_model('test')
537+
operations = [
538+
migrations.AddField(
539+
model_name='Pony',
540+
name='remote',
541+
field=models.IntegerField(unique=True, null=True),
542+
),
543+
migrations.CreateModel(
544+
'Rider',
545+
[
546+
('id', models.AutoField(primary_key=True)),
547+
('pony_remote', models.ForeignKey('Pony', on_delete=models.CASCADE, to_field="remote")),
548+
],
549+
),
550+
]
551+
with self.collect_sql() as sqls:
552+
self.apply_operations('test', new_state, operations)
553+
554+
if TEST_WITH_POSTGRES:
555+
identity = "serial"
556+
elif TEST_WITH_REDSHIFT:
557+
identity = "integer identity(1, 1)"
558+
559+
self.assertEqual([
560+
'''ALTER TABLE "test_pony" ADD COLUMN "remote" integer NULL;''',
561+
f'''CREATE TABLE "test_rider" ("id" {identity} NOT NULL PRIMARY KEY, "pony_remote_id" integer NOT NULL) ;''',
562+
'''ALTER TABLE "test_pony" ADD CONSTRAINT "test_pony_remote_e347b432_uniq" UNIQUE ("remote");''',
563+
'''ALTER TABLE "test_rider" ADD CONSTRAINT "test_rider_pony_remote_id_269d66d9_fk_test_pony_remote" FOREIGN KEY ("pony_remote_id") REFERENCES "test_pony" ("remote");'''
564+
], sqls)

0 commit comments

Comments
 (0)