Django PostgreSQL IntegerRangeField 和 update_or_create
Django PostgreSQL IntegerRangeField and update_or_create
我有一个非常简单的模型:
from django.contrib.postgres.fields import IntegerRangeField
from django.db import models
class Production(models.Model):
title = models.CharField(max_length=100, blank=True, db_index=True)
year = models.PositiveSmallIntegerField(blank=True, default=0, db_index=True)
index = models.PositiveSmallIntegerField(blank=True, default=0)
run_years = IntegerRangeField(blank=True, null=True)
class Meta:
unique_together = ('title', 'year', 'index')
但是,当我通过 update_or_create
方法操纵我的模型时,我遇到了一个非常奇怪的行为。出于某种原因,我的 IntegerRange
字段被擦掉了。
In [1]: Production.objects.update_or_create(**{'title': '#twentyfiveish', 'year': 2017,
'index': 0, 'run_years': (2017, 2017)})[0].run_years
Out[1]: (2017, 2017)
In [2]: Production.objects.update_or_create(**{'title': '#twentyfiveish', 'year': 2017,
'index': 0, 'run_years': (2017, 2017)})[0].run_years
Out[2]: NumericRange(empty=True)
在我看来,这不是我想要的行为。是不是错误?请指教
版本:
- Django 1.11
- PostgreSQL 9.6.3
不幸的是,这是 "as designed"。放在一起
Regardless of the bounds specified when saving the data, PostgreSQL
always returns a range in a canonical form that includes the lower
bound and excludes the upper bound; that is [).
和
-- includes no points (and will be normalized to 'empty')
SELECT '[4,4)'::int4range;
(来自 https://www.postgresql.org/docs/9.3/static/rangetypes.html)
你运气不好。但是,有一个功能请求,https://code.djangoproject.com/ticket/27147。
顺便说一句,psycopg2 支持所有范围边界类型。
选项?
- 一次性定制 sql(恶心)
- 实施 Django 功能请求并提交 PR
- 只需使用 2 个 int 列(最快)
我有一个非常简单的模型:
from django.contrib.postgres.fields import IntegerRangeField
from django.db import models
class Production(models.Model):
title = models.CharField(max_length=100, blank=True, db_index=True)
year = models.PositiveSmallIntegerField(blank=True, default=0, db_index=True)
index = models.PositiveSmallIntegerField(blank=True, default=0)
run_years = IntegerRangeField(blank=True, null=True)
class Meta:
unique_together = ('title', 'year', 'index')
但是,当我通过 update_or_create
方法操纵我的模型时,我遇到了一个非常奇怪的行为。出于某种原因,我的 IntegerRange
字段被擦掉了。
In [1]: Production.objects.update_or_create(**{'title': '#twentyfiveish', 'year': 2017,
'index': 0, 'run_years': (2017, 2017)})[0].run_years
Out[1]: (2017, 2017)
In [2]: Production.objects.update_or_create(**{'title': '#twentyfiveish', 'year': 2017,
'index': 0, 'run_years': (2017, 2017)})[0].run_years
Out[2]: NumericRange(empty=True)
在我看来,这不是我想要的行为。是不是错误?请指教
版本:
- Django 1.11
- PostgreSQL 9.6.3
不幸的是,这是 "as designed"。放在一起
Regardless of the bounds specified when saving the data, PostgreSQL always returns a range in a canonical form that includes the lower bound and excludes the upper bound; that is [).
和
-- includes no points (and will be normalized to 'empty')
SELECT '[4,4)'::int4range;
(来自 https://www.postgresql.org/docs/9.3/static/rangetypes.html)
你运气不好。但是,有一个功能请求,https://code.djangoproject.com/ticket/27147。
顺便说一句,psycopg2 支持所有范围边界类型。
选项?
- 一次性定制 sql(恶心)
- 实施 Django 功能请求并提交 PR
- 只需使用 2 个 int 列(最快)