{{{#!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.
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>
--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:2>
* 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>
* status: closed => new
* resolution: fixed =>
--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:4>
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32967#comment:5>
* 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>
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>
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>
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>