我有一个带有以下型号的django应用程序:
class Event(models.Model):
# id = models.IntegerField(primary_key=True)
event_type = models.ForeignKey(
EventType,
on_delete=models.DO_NOTHING,
# we don't want to delete anything in the StatusType table.
blank=True,
null=True,
)
reason_code = models.IntegerField(default=0)
elapsed_time = models.IntegerField(default=0)
event_at = models.DateTimeField("Event Time", blank=True, auto_now_add=True)
object_id = models.ForeignKey(Machine, on_delete=models.DO_NOTHING, )
count = models.BigIntegerField(blank=True, null=True)
position = models.IntegerField(blank=True, null=True)
rfid_tag = models.ForeignKey(Employee, blank=True, null=True, on_delete=models.DO_NOTHING,
verbose_name='User Name')
hidden = models.BooleanField(default=False) # for hiding from lists
# def __str__(self): # __unicode__ on Python 2
# return self.event_type
class Meta:
verbose_name = 'event'
verbose_name_plural = 'Events'
def __str__(self):
return str(self.id)
在尝试运行简单迁移时,我不断收到此错误:
File "/home/icar/E-Django/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/epicar/E-Django/venv/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type double precision to time without time zone
我遇到的问题是,我不明白为什么它试图将双倍精度计算到没有时区的时间。我不想要它。我不太确定还有什么可以尝试的。我尝试过以下操作:
SELECT CAST(elapsed_time as double precision) FROM event;
我也尝试将elapsed_time字段更改为整数并得到相同的错误(引用整数而不是双精度)。所以我知道那是特定的领域。
我删除了所有迁移并尝试重新开始。那没用。
有什么想法吗?
更新:来自pgadmin的已用字段的属性:
答案 0 :(得分:0)
如果我理解正确,你会找到这个:
SELECT extract(epoch FROM now());
答案 1 :(得分:0)
想出来。 :)它最终出现在Django方面:
字段类型更改:
elapsed_time = models.TimeField(blank=True, auto_now_add=True)
这解决了Django和Postgres字段类型之间的冲突。
System check identified no issues (0 silenced).
February 09, 2017 - 16:34:59
Django version 1.9, using settings 'E-Django.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
所有好的,现在正在运行。