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

Ensure custom arguments for fields are included in historical models' fields #462

Merged
merged 1 commit into from
Nov 8, 2018

Conversation

tbeadle
Copy link
Contributor

@tbeadle tbeadle commented Oct 20, 2018

Description

Previously, if a custom field class was used for a foreign key and that
class accepted custom kwargs, those kwargs would not be included in the
creation of the historical model's field and would break the migration.
To address this, we'll use the deconstruct() method of the field to get
the arguments that should be passed to it and override the ones we need
to for the historical model's field.

Related Issue

Fixes #431

Motivation and Context

See description

How Has This Been Tested?

Added unit tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codecov-io
Copy link

codecov-io commented Oct 20, 2018

Codecov Report

Merging #462 into master will increase coverage by 0.09%.
The diff coverage is 96.96%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #462      +/-   ##
==========================================
+ Coverage   97.09%   97.19%   +0.09%     
==========================================
  Files          15       16       +1     
  Lines         689      713      +24     
  Branches       96       94       -2     
==========================================
+ Hits          669      693      +24     
  Misses         10       10              
  Partials       10       10
Impacted Files Coverage Δ
...tomattrforeignkey_modelwithcustomattrforeignkey.py 100% <100%> (ø)
simple_history/models.py 99.2% <100%> (+0.38%) ⬆️
simple_history/registry_tests/tests.py 95.69% <100%> (+0.19%) ⬆️
...istory/registry_tests/migration_test_app/models.py 95.65% <92.85%> (-4.35%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3183611...84ceecc. Read the comment docs.

@@ -178,29 +178,18 @@ def copy_fields(self, model):
field.__class__ = models.IntegerField
if isinstance(field, models.ForeignKey):
old_field = field
field_arguments = {'db_constraint': False}
old_field.swappable = False
name, path, args, field_arguments = old_field.deconstruct()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make name and path underscores since they aren't used?

@@ -178,29 +178,18 @@ def copy_fields(self, model):
field.__class__ = models.IntegerField
if isinstance(field, models.ForeignKey):
old_field = field
field_arguments = {'db_constraint': False}
old_field.swappable = False
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's change this back to its original setting after calling deconstruct

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tbeadle let's change the swappable setting back after doing this deconstruct? And I'd love to figure out why those fields need to be sorted in tests – haven't been able to find why the order changes but want to know before merging. I'll look into it some more

… fields.

Previously, if a custom field class was used for a foreign key and that
class accepted custom kwargs, those kwargs would not be included in the
creation of the historical model's field and would break the migration.
To address this, we'll use the deconstruct() method of the field to get
the arguments that should be passed to it and override the ones we need
to for the historical model's field.
Copy link
Collaborator

@rossmechanic rossmechanic left a comment

Choose a reason for hiding this comment

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

lgtm thanks @tbeadle

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.

django-simple-history does not respect 'deconstruct' kwargs for custom foreignkey fields
3 participants