[Django] #32967: Buggy Migrations when `flota("nan")` in `Q` objects

4 views
Skip to first unread message

Django

unread,
Jul 27, 2021, 6:06:38 PM7/27/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `flota("nan")` in `Q` objects
-------------------------------------+-------------------------------------
Reporter: Steven | Owner: Steven Jin
Jin |
Type: Bug | Status: assigned
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords: db Q
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When creating a `CheckConstraint` in the form

{{{#!python
class MyModel
x = FloatField()
class Meta:
constraints = [~CheckConstraint(check=Q(x=float("nan")), name="some
constraint")]
}}}

This constraint is removed and added back every time `python manage.py
makemigrations` is run. This is because the inherited `__eq__` function of
`Q` objects does not take into account the fact that `float("nan") !=
float("nan")`.

--
Ticket URL: <https://code.djangoproject.com/ticket/32967>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 27, 2021, 6:07:04 PM7/27/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `flota("nan")` in `Q` objects
-------------------------------------+-------------------------------------
Reporter: Steven Jin | Owner: Steven

| Jin
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:

Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Steven Jin:

Old description:

> When creating a `CheckConstraint` in the form
>
> {{{#!python
> class MyModel
> x = FloatField()
> class Meta:
> constraints = [~CheckConstraint(check=Q(x=float("nan")), name="some
> constraint")]
> }}}
>
> This constraint is removed and added back every time `python manage.py
> makemigrations` is run. This is because the inherited `__eq__` function
> of `Q` objects does not take into account the fact that `float("nan") !=
> float("nan")`.

New description:

When creating a `CheckConstraint` in the form

{{{#!python
class MyModel(Model):


x = FloatField()
class Meta:
constraints = [~CheckConstraint(check=Q(x=float("nan")), name="some
constraint")]
}}}

This constraint is removed and added back every time `python manage.py
makemigrations` is run. This is because the inherited `__eq__` function of
`Q` objects does not take into account the fact that `float("nan") !=
float("nan")`.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:1>

Django

unread,
Jul 27, 2021, 6:11:07 PM7/27/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `float("nan")` in `Q` objects
-------------------------------------+-------------------------------------
Reporter: Steven Jin | Owner: Steven

| Jin
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:2>

Django

unread,
Jul 27, 2021, 6:26:46 PM7/27/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `float("nan")` in `Q` objects
-------------------------------------+-------------------------------------

Reporter: Steven Jin | Owner: Steven
| Jin
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Steven Jin):

* cc: Steven Jin (added)
* status: assigned => closed
* has_patch: 0 => 1
* resolution: => fixed


Old description:

> When creating a `CheckConstraint` in the form
>
> {{{#!python

> class MyModel(Model):


> x = FloatField()
> class Meta:
> constraints = [~CheckConstraint(check=Q(x=float("nan")), name="some
> constraint")]
> }}}
>
> This constraint is removed and added back every time `python manage.py
> makemigrations` is run. This is because the inherited `__eq__` function
> of `Q` objects does not take into account the fact that `float("nan") !=
> float("nan")`.

New description:

When creating a `CheckConstraint` in the form

{{{#!python
class MyModel(Model):


x = FloatField()
class Meta:
constraints = [~CheckConstraint(check=Q(x=float("nan")), name="some
constraint")]
}}}

This constraint is removed and added back every time `python manage.py
makemigrations` is run. This is because the inherited `__eq__` function of
`Q` objects does not take into account the fact that `float("nan") !=
float("nan")`.

See PR: https://github.com/django/django/pull/14706

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:3>

Django

unread,
Jul 27, 2021, 6:27:19 PM7/27/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `float("nan")` in `Q` objects
-------------------------------------+-------------------------------------

Reporter: Steven Jin | Owner: Steven
| Jin
Type: Bug | Status: new

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Steven Jin):

* status: closed => new
* resolution: fixed =>


--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:4>

Django

unread,
Jul 27, 2021, 6:27:44 PM7/27/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `float("nan")` in `Q` objects
-------------------------------------+-------------------------------------
Reporter: Steven Jin | Owner: Steven

| Jin
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Steven Jin):

* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:5>

Django

unread,
Jul 28, 2021, 1:29:42 AM7/28/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `float("nan")` in `Q` objects
-------------------------------------+-------------------------------------

Reporter: Steven Jin | Owner: Steven
| Jin
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: assigned => closed

* resolution: => wontfix


Comment:

Thanks for the report, however it looks like a hypothetical issue, what do
you want to check with such constraint? (`float('nan') != whatever)`.
`FloatField` uses a proper data type and this should be ensured on a
database layer without extra constraints. I don't think it's worth
additional complexity.

--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:6>

Django

unread,
Jul 28, 2021, 10:01:10 AM7/28/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `float("nan")` in `Q` objects
-------------------------------------+-------------------------------------

Reporter: Steven Jin | Owner: Steven
| Jin
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Steven Jin):

Thanks for the reply! How would I ensure that values in a column are not
`nan`?

--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:7>

Django

unread,
Jul 28, 2021, 10:43:55 AM7/28/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `float("nan")` in `Q` objects
-------------------------------------+-------------------------------------

Reporter: Steven Jin | Owner: Steven
| Jin
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Have you tried to simply compare against the `NaN` string?

{{{#!python
~CheckConstraint(check=Q(x='NaN'), name="not_nan")
}}}

It seems to work for me on PostgreSQL.

--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:8>

Django

unread,
Jul 28, 2021, 2:19:53 PM7/28/21
to django-...@googlegroups.com
#32967: Buggy Migrations when `float("nan")` in `Q` objects
-------------------------------------+-------------------------------------

Reporter: Steven Jin | Owner: Steven
| Jin
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: db Q | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Steven Jin):

Thanks!

Replying to [comment:8 Simon Charette]:


> Have you tried to simply compare against the `NaN` string?
>
> {{{#!python
> ~CheckConstraint(check=Q(x='NaN'), name="not_nan")
> }}}
>
> It seems to work for me on PostgreSQL.

--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:9>

Reply all
Reply to author
Forward
0 new messages