Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#32967 closed Bug (wontfix)

Buggy Migrations when `float("nan")` in `Q` objects

Reported by: Steven Jin Owned by: Steven Jin
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords: db Q
Cc: Steven Jin Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Steven Jin)

When creating a CheckConstraint in the form

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

Change History (9)

comment:1 by Steven Jin, 3 years ago

Description: modified (diff)

comment:2 by Steven Jin, 3 years ago

Summary: Buggy Migrations when `flota("nan")` in `Q` objectsBuggy Migrations when `float("nan")` in `Q` objects

comment:3 by Steven Jin, 3 years ago

Cc: Steven Jin added
Description: modified (diff)
Has patch: set
Resolution: fixed
Status: assignedclosed

comment:4 by Steven Jin, 3 years ago

Resolution: fixed
Status: closednew

comment:5 by Steven Jin, 3 years ago

Status: newassigned

comment:6 by Mariusz Felisiak, 3 years ago

Resolution: wontfix
Status: assignedclosed

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.

comment:7 by Steven Jin, 3 years ago

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

comment:8 by Simon Charette, 3 years ago

Have you tried to simply compare against the NaN string?

~CheckConstraint(check=Q(x='NaN'), name="not_nan")

It seems to work for me on PostgreSQL.

in reply to:  8 comment:9 by Steven Jin, 3 years ago

Thanks!

Replying to Simon Charette:

Have you tried to simply compare against the NaN string?

~CheckConstraint(check=Q(x='NaN'), name="not_nan")

It seems to work for me on PostgreSQL.

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