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 #26739 -- Made reverse RemoveField handle non-nullable columns. #16890

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

DevilsAutumn
Copy link
Contributor

@DevilsAutumn DevilsAutumn commented May 23, 2023

Ticket #26739
Thanks @MarkusH , @apollo13 and @Gagaro for initial work. (#6870 )

checklist:

  • Add test for ask_not_null_removal questioner.

@DevilsAutumn DevilsAutumn marked this pull request as draft May 23, 2023 14:40
django/db/migrations/operations/fields.py Outdated Show resolved Hide resolved
@DevilsAutumn DevilsAutumn force-pushed the ticket_26739 branch 2 times, most recently from 4517c00 to 2af2a74 Compare May 23, 2023 17:43
@DevilsAutumn DevilsAutumn marked this pull request as ready for review May 23, 2023 19:17
@DevilsAutumn DevilsAutumn changed the title WIP #26739 -- Made reverse RemoveField handle non-nullable columns. Fixed #26739 -- Made reverse RemoveField handle non-nullable columns. May 23, 2023
and not field.many_to_many
and not (field.blank and field.empty_strings_allowed)
and not (
isinstance(field, time_fields)
Copy link
Contributor

Choose a reason for hiding this comment

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

Checking the type is unnecessary. Often duck typing is used in Django; presence of auto_now or auto_now_add is enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I am not wrong fields other than date and time fields don't have auto_now and auto_now_add attribute. won't they throw an error if i remove type checking? It is used even in field addition, see here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe let's use:

getattr(field, "auto_now", False) or getattr(field, "auto_now_add", False)

This is the same as BaseDatabaseSchemaEditor._effective_default.
I think we have everything from BaseDatabaseSchemaEditor._effective_default, I tested GeneratedField case but these are covered by not field.null.

Comment on lines +1929 to +1955
# MySQL populates NOT NULL cols with implicit defaults instead of raising
# an error.
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting I did not know this… I don't use MySQL but I'm hardly surprised 😛

I wonder whether this can be utilised in any way.

Copy link
Contributor

@LilyFoote LilyFoote left a comment

Choose a reason for hiding this comment

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

Looks like a good start to me!

The main thing I spotted is that we don't have any support for db_default which landed recently.

django/db/migrations/autodetector.py Show resolved Hide resolved
django/db/migrations/operations/fields.py Show resolved Hide resolved
django/db/migrations/operations/fields.py Show resolved Hide resolved
django/db/migrations/questioner.py Show resolved Hide resolved
Copy link
Contributor

@sarahboyce sarahboyce left a comment

Choose a reason for hiding this comment

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

Thank you @DevilsAutumn! This is getting really close, got a few more comments 👍

django/db/migrations/questioner.py Outdated Show resolved Hide resolved
docs/releases/5.0.5.txt Outdated Show resolved Hide resolved
docs/releases/5.1.txt Outdated Show resolved Hide resolved
and not field.many_to_many
and not (field.blank and field.empty_strings_allowed)
and not (
isinstance(field, time_fields)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe let's use:

getattr(field, "auto_now", False) or getattr(field, "auto_now_add", False)

This is the same as BaseDatabaseSchemaEditor._effective_default.
I think we have everything from BaseDatabaseSchemaEditor._effective_default, I tested GeneratedField case but these are covered by not field.null.

django/db/migrations/autodetector.py Show resolved Hide resolved
tests/migrations/test_operations.py Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants