失败加载数据夹具Django - 整数超出范围

时间:2015-02-16 06:09:55

标签: django postgresql

我正在使用Django和Postgre开发应用程序。

基本上,我在我的应用上创建了新模型。

from django.db import models
from manager.models.state import State
from django.contrib.auth.models import User

class Address (models.Model):
 address_id = models.AutoField(primary_key=True)
 address_name = models.CharField(max_length=500)
 address_city = models.CharField(max_length=250)
 address_phone = models.IntegerField()
 address_postcode = models.CharField(max_length=10, default = 0)
 state =  models.ForeignKey(State, null=True, blank=True, default = None)
 user = models.ForeignKey(User,  null=True, blank=True, default = None)

def __unicode__(self):
    return self.address_id

@classmethod
def get_list(cls):
    return list(cls.objects.values())

之后我运行makemigrations并迁移命令行。所有模型都是在数据库中正常创建的。

我成功加载了其他json文件(fixture)的数据。 当我试图在地址表上loaddata address.json时,我收到了这个错误。

错误讯息:

django.db.utils.DataError: Problem installing fixture '/Users/eeldwin/Documents/Django/fbt/manager/fixtures/address.json': Could not load manager.Address(pk=1): integer out of range

追溯:

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
   execute_from_command_line(sys.argv)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django  /core/management/__init__.py", line 385, in execute_from_command_line
   utility.execute()
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
   self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
   self.execute(*args, **options.__dict__)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
   output = self.handle(*args, **options)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 61, in handle
   self.loaddata(fixture_labels)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 91, in loaddata
    self.load_label(fixture_label)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 148, in load_label
    obj.save(using=self.using)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/core/serializers/base.py", line 173, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/models/base.py", line 679, in _save_table
    forced_update)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/models/base.py", line 723, in _do_update
    return filtered._update(values) > 0
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/models/query.py", line 600, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/eeldwin/.virtualenvs/fbt/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: Problem installing fixture '/Users/eeldwin/Documents/Django/fbt/manager/fixtures/address.json': Could not load manager.Address(pk=1): integer out of range

这是我的灯具

[{
"model": "manager.address",
"pk": 1,
"fields": {
  "address_id": "1",
  "address_name": "Jalan Muara Mas Timur 242",
  "address_city": "Semarang",
  "address_phone": "087832270893",
  "address_postcode": "3122",
  "state": "1"
}}]

但是,当我手动将数据插入数据库时​​,它将完美运行。有什么想法在这里发生了什么?谢谢。

1 个答案:

答案 0 :(得分:1)

您将address_phone存储为IntegerField。其最大值为2147483647(https://docs.djangoproject.com/en/1.7/ref/models/fields/#integerfield)。

但是在您的灯具中,您尝试将087832270893添加为address_phone,这就是您收到integer out of range错误的原因。