2010-06-04 114 views
0

我有以下描述的模型:Django的:不能将数据保存到模型,PSQL错误

class UserProfile(models.Model): 
    avatar = models.ImageField(blank = True, upload_to='files') 
    about = models.TextField(blank=True) 
    rank = models.IntegerField(default = 1) 
    solvedProblems = models.ManyToManyField(Composition, blank=True) 
    user = models.ForeignKey(User, unique=True) 
    country = CountryField(blank = True) 
    status = models.ForeignKey(UserRank) 

其中UserRank是:

class UserRank(models.Model): 
    rankName = models.CharField(max_length = 300) 
    upLimit = models.IntegerField() 
    downLimit = models.IntegerField() 

我已经添加了状态,国家和头像场稍后在描述模型之后,通过sql更新了数据库。

现在看起来数据库是这样的:

chess_problems =#\ d registration_userprofile; 表“public.registration_userprofile” 列|类型|修改器
----------- + ------------------------ + ---------- -------------------------------------------------- ----------- id |整数| not null default nextval('registration_userprofile_id_seq':: regclass) 关于|文字|非空 等级|整数|非null user_id |整数|非空 头像|字符变化(100)| 国家|字符变化(2)| status_id |整数| 索引: “registration_userprofile_pkey” 主键,B树(ID) “registration_userprofile_user_id_key” UNIQUE,B树(USER_ID) 外键约束: “registration_userprofile_status_id_fkey” 外键(STATUS_ID)参考registration_userrank(ID)DEFERRABLE INITIALLY DEFERRED “registration_userprofile_user_id_fkey “外键(USER_ID)参考文献AUTH_USER(ID)DEFERRABLE INITIALLY DEFERRED

错误代码,我看到的是:

(, DataError('value too long for type character varying(2)\n',),)

回溯(最近通话最后一个): 文件“/usr/lib/python2.5/site-packages/Django-1.1.1-py2.5.egg/django/core/servers/basehttp.py”,行280,运行 self.finish_response() 文件“/usr/lib/python2.5/site-packages/Django-1.1.1-py2.5.egg/django/core/servers/basehttp.py”,第320行,在finish_response中 self.write(data ) 文件“/usr/lib/python2.5/site-packages/Django-1.1.1-py2.5.egg/django/core/servers/basehttp.py”,第416行,写入 self._write(数据) 文件“/usr/lib/python2.5/socket.py”,第274行,写入 self.flush() 文件“/usr/lib/python2.5/socket.py”,第261行, in flush self._sock.sendall(buffer) error:(32,'Broken pipe')

我觉得发生这种情况是因为我错误地更新了数据库以适合模型......但不知道什么是错的,以及如何解决它。相同的代码在mysql实例本地工作...但我有psql上prod ....

+0

请问您可以缩进一下您的代码或数据库架构吗? – dzen 2010-06-04 06:53:46

+0

如何定义'CountryField'?在数据库中,它看起来就是'字符变化(2)'的那个。你是否在该字段中插入了更长的字符串? – tcarobruce 2010-06-04 07:15:40

+0

非常感谢人。我增加了存储国家代码的领域,胜利是我的!!!!! – 2010-06-04 13:15:53

回答

2

我打赌你使用这个:http://djangosnippets.org/snippets/494/和[as tcarobruce说] CountryField是一个2个字符的代码,代表这个国家 - 'FR','GB','US'等等,而你试图存储国家的完整字符串名称。