Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix safer alter field set not null for fk fields #75

Merged
merged 2 commits into from
Feb 10, 2025

Conversation

marcelofern
Copy link
Contributor

Changes:

Fix SaferAlterFieldSetNotNull for custom-named fields

Prior to this change, this operation didn't work for fields that had
column names in the database that didn't match their names (such as
ForeignKey fields, which have an "_id" suffix).

This commit changes that by obtaining the column name from the model's
meta class instead.

Copy link

Coverage Report Results

Name Stmts Miss Branch BrPart Cover
src/django_pg_migration_tools/indexes.py 9 0 0 0 100%
src/django_pg_migration_tools/management/commands/migrate_with_timeouts.py 138 0 20 0 100%
src/django_pg_migration_tools/operations.py 452 0 90 0 100%
src/django_pg_migration_tools/timeouts.py 73 0 30 0 100%
TOTAL 672 0 140 0 100%

3 empty files skipped.

@marcelofern marcelofern requested a review from a team February 10, 2025 01:17
@marcelofern marcelofern marked this pull request as ready for review February 10, 2025 01:17
Copy link

@timb07 timb07 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

""")

@pytest.mark.django_db(transaction=True)
def test_when_field_is_a_foreign_key(self):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test name got switched with the one for the new test above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It did! Will amend.

The SaferAlterFieldSetNotNull operation fails to run when
the field field is a models.ForeignKey

This is because in a Django model, the name of a Python field may be
different from the name of the corresponding table column.

One such example is the ForeignKey field, which receives an "_id"
suffix in the database column name.
Prior to this change, this operation didn't work for fields that had
column names in the database that didn't match their names (such as
ForeignKey fields, which have an "_id" suffix).

This commit changes that by obtaining the column name from the model's
meta class instead.
@marcelofern marcelofern force-pushed the fix-safer-alter-field-set-not-null-for-fk-fields branch from 4cd1556 to c468c0c Compare February 10, 2025 23:01
@marcelofern marcelofern merged commit 52b778d into main Feb 10, 2025
11 checks passed
@marcelofern marcelofern deleted the fix-safer-alter-field-set-not-null-for-fk-fields branch February 10, 2025 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants