Opened 3 years ago

Closed 3 years ago

#33605 closed Bug (fixed)

Use of RegexValidator can cause migration generation to raise AttributeError

Reported by: Brian Helba Owned by: Brian Helba
Component: Migrations Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If a model:

my_field = CharField(
    max_length=64,
    validators=[RegexValidator('pattern')],
)

is migrated to:

my_field = CharField(
    max_length=64,
    validators=[RegexValidator(re.compile('pattern'))],
)

an uncaught AttributeError: 'str' object has no attribute 'pattern' will be raised from here: https://github.com/django/django/blob/59ab3fd0e9e606d7f0f7ca26609c06ee679ece97/django/db/migrations/utils.py#L18 ,
caused by this comparison: https://github.com/django/django/blob/59ab3fd0e9e606d7f0f7ca26609c06ee679ece97/django/db/migrations/autodetector.py#L1143

This is because comparing a RegexObject (the wrapper for a compiled regex) to a string is not properly handled. This issue was introduced by https://github.com/django/django/commit/91f701f4fc324cd2feb7dbf151338a358ca0ea18

Change History (4)

comment:1 by Brian Helba, 3 years ago

Owner: changed from nobody to Brian Helba
Status: newassigned

comment:2 by Mariusz Felisiak, 3 years ago

Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Thanks for the report.

PR

comment:3 by Mariusz Felisiak, 3 years ago

Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 2d5215c:

Fixed #33605 -- Fixed migration crash when altering RegexValidator to pre-compiled regular expression.

Note: See TracTickets for help on using tickets.
Back to Top