Django工具失败,说明“数据库错误:值太长,类型字符变化(50)” [英] Django fixture fails, stating "DatabaseError: value too long for type character varying(50)"

查看:494
本文介绍了Django工具失败,说明“数据库错误:值太长,类型字符变化(50)”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个fixture(json)在开发环境中加载,但是在服务器环境中却没有这样做。错误说: DatabaseError:值太长,类型字符变化(50)



我的开发环境是Windows& Postgres 8.4。服务器运行Debian和Postgres 8.3。数据库编码在两个系统中都是UTF8。



就像夹具中的unicode标记计数为服务器上的字符一样,它们会导致一些字符串超出其字段的最大长度。但是,在开发环境中并不会发生这种情况。

解决方案

嗯,区别在于模板数据库的编码。在生产服务器上,它们具有ascii编码,而在dev框上则是utf-8。



默认情况下,postgres使用template1创建一个数据库。我的理解是,如果它的编码不是utf-8,那么你创建的数据库将会有这个问题,即使你使用utf-8编码创建它。



因此我删除它并重新创建它的编码设置为UTF8。下面的代码片段(取自 here ):

  psql -U postgres 

更新pg_database SET datallowconn = TRUE其中datname ='template0';
\c template0
更新pg_database SET datistemplate = FALSE其中datname ='template1';
drop database template1;
创建数据库template1 with template = template0 encoding ='UNICODE';
更新pg_database SET datistemplate = TRUE其中datname ='template1';
\c template1
更新pg_database SET datallowconn = FALSE其中datname ='template0';

现在夹具平稳地加载。


I have a fixture (json) which loads in development environment but fails to do so in server environment. The error says: "DatabaseError: value too long for type character varying(50)"

My development environment is Windows & Postgres 8.4. The server runs Debian and Postgres 8.3. Database encoding is UTF8 in both systems.

It is as if unicode markers in the fixture count as chars on the server and they cause some strings to exceed their field's max length. However that does not happen in the dev environment..

解决方案

Well, what makes the difference is the encoding of the template databases. On the production server they had ascii encoding while on the dev box it is utf-8.

By default postgres creates a database using the template1. My understanding is that if its encoding is not utf-8, then the database you create will have this issue, even though you create it with utf-8 encoding.

Therefore I dropped it and recreated it with its encoding set to UTF8. The snippet below does it (taken from here):

psql -U postgres 

UPDATE pg_database SET datallowconn = TRUE where datname = 'template0';
\c template0
UPDATE pg_database SET datistemplate = FALSE where datname = 'template1';
drop database template1;
create database template1 with template = template0 encoding = 'UNICODE';
UPDATE pg_database SET datistemplate = TRUE where datname = 'template1';
\c template1
UPDATE pg_database SET datallowconn = FALSE where datname = 'template0';

Now the fixture loads smoothly.

这篇关于Django工具失败,说明“数据库错误:值太长,类型字符变化(50)”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆