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

Fixed #23794 - FieldDoesNotExist error in migration with deleted fields and unique_together constraint #3533

Closed
wants to merge 1 commit into from

Conversation

apragacz
Copy link
Contributor

The code in check_dependency is too specific. For instance the any function for operation.unique_together=set([]) always returned False which caused the bug to happen. This PR contains following changes:

  • added test case for this bug
  • added simplified condition

https://code.djangoproject.com/ticket/23794

@apragacz
Copy link
Contributor Author

@andrewgodwin should I also send the patch to the Trac?

@charettes
Copy link
Member

@szopu you just have to add a comment in the ticket linking to your PR (I just did it)

@charettes
Copy link
Member

Buildbot, test this please.

return (
isinstance(operation, (operations.AlterUniqueTogether,
operations.AlterIndexTogether)) and
operation.name.lower() == dependency[1].lower()
Copy link
Member

Choose a reason for hiding this comment

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

I included the any() call to prevent problems with RenameField operations that that involve a field that's in an foo_together. Turns out, the RenameField handles the state changes itself. No need to check for it here.

@apragacz
Copy link
Contributor Author

As a newbie, I have no idea why the build failed... I ran the tests using the python runtests.py locally and everything was OK (also created virtualenvs for py2, and py3, where I installed the requirements & "editable" django and ran the tests - same result).

@MarkusH
Copy link
Member

MarkusH commented Nov 18, 2014

buildbot, test this please.

@MarkusH
Copy link
Member

MarkusH commented Nov 18, 2014

@szopu I think the errors were related to some errors on the master branch. They aren't related to your commit, I think.

autodetector = MigrationAutodetector(before, after)
changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['otherapp']), 1)
Copy link
Member

Choose a reason for hiding this comment

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

Can you use self.assertNumberMigrations(), self.assertOperationTypes() and self.assertOperationAttributes() instead of things like self.assertEqual(first_action.__class__.__name__, "AlterUniqueTogether"), please. Have a look through at the top of the test case on which arguments they expect and how they work. They are also heavily used all over the place in the autodetector tests. (A overall cleanup is in #3564.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! But I guess I should wait with it till the PR #3564 with the refactor will be merged into master?

Copy link
Member

Choose a reason for hiding this comment

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

Hehe, if you don't mind, sure :) In that case two additional comments:

  • Rename the test to test_foo_together_remove_fk
  • Use self.book_foo_together instead of self.book_unique

@apragacz
Copy link
Contributor Author

@MarkusH rebased the code to current master & rewritten the test according to you suggestions.

The only problem right now is that using self.book_foo_together causes the autodectector to spit out additional AlterIndexTogether. I assumed in the test the order of AlterUniqueTogether and AlterIndexTogether which does not need to hold in the future. I could add some branching for specific cases in the test but I'm not sure if this is the right idea.

@MarkusH
Copy link
Member

MarkusH commented Nov 20, 2014

As of now AlterUniqueTogether is always before AlterIndexTogether, although they don't have any inter-dependencies to guarantee that, just by adding the unique operation before the index operation (https://github.com/szopu/django/blob/ticket_23794/django/db/migrations/autodetector.py#L185-L186).

Just check for them as done in https://github.com/szopu/django/blob/ticket_23794/tests/migrations/test_autodetector.py#L895-L897

@MarkusH
Copy link
Member

MarkusH commented Nov 20, 2014

buildbot, test this please.

def test_foo_together_remove_fk(self):
"Tests unique_together and field removal detection & ordering"
# Make state
before = self.make_project_state([self.author_empty,
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably keep those lines unwrapped. Maximum line length is at 119 chars.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm accustomed to the PEP8 79 chars line limit (this is really helpful when using multiple columns for instance in Sublime Text). I know that django does not strictly follow this rule. On the other hand, the code is still readable IMO, so I would leave it as it is now.

@MarkusH
Copy link
Member

MarkusH commented Nov 20, 2014

Patch looks good to me, @szopu .

Last but not least, can you add a line to the 1.7.2 release notes (append it at the end). You can find the release notes in docs/releases/1.7.2.txt. This patch should be backported because #23614 was backported too.

@timgraham
Copy link
Member

Added release note and merged in 72729f8, thanks!

@timgraham timgraham closed this Nov 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants