Django表单/数据库错误:值太长,类型字符变化(4) [英] Django form/database error: value too long for type character varying(4)

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

问题描述

我要做的是将一个条带(记帐服务)公司ID [大约200个字符左右]保存到Django中的数据库。如何启用Django允许更长的值?



具体错误是:
数据库错误:类型字符变长时值太长(4)



我看到:
值太长,类型字符变化(N )
和:
Django夹具失败,说明DatabaseError:value too long for type character varying(50)




  • 我的数据库已经编码为UTF-8,根据我的webhost。
    EDIT - 我看到一个答案建议使列更宽。这是否涉及修改PostgreSQL数据库?



我的具体系统是Webfaction,CentOs共享机器,Django运行在PostgreSQL上。

解决方案

是的,使列更宽。错误消息很清楚:您的200个字符太大,不适合varchar(4)。



首先,将您的模型字段 max_length 更新为您期望的数字,您要喂它的数据。



接下来,您必须将数据库列本身更新为 django不会自动更新现有列



这里有几个选项:



1:
删除数据库并再次运行syncdb。警告:您将丢失所有数据。



2:通过SQL手动更新列。



输入 python manage.py dbshel​​l 进入您的数据库shell并输入

  ALTER TABLE my_table ALTER COLUMN my_column TYPE VARCHAR(200)

3:学习和使用数据库迁移工具例如 django south ,这将有助于使您的数据库随您的型号代码更新。


What I'm trying to do is save a stripe (the billing service) company id [around 200 characters or so] to my database in Django. How can I enable Django to allow for longer values?

The specific error is: database error: value too long for type character varying(4)

I saw: value too long for type character varying(N) and: Django fixture fails, stating "DatabaseError: value too long for type character varying(50)"

  • my database is already encoded for UTF-8, according to my webhost. EDIT - I see that one answer recommends making the column wider. Does that involve modifying the PostgreSQL database?

My specific system is Webfaction, CentOs shared machine, Django running on PostgreSQL. I would really appreciate a conceptual overview of what's going on and how I can fix it.

解决方案

Yes, make the column wider. The error message is quite clear: your 200 characters are too big to fit in a varchar(4).

First, update your model fields max_length attribute from 4 to a number that you expect will be long enough to contain the data you're feeding it.

Next up you have to update the database column itself as django will not automatically update existing columns.

Here are a few options:

1: Drop the database and run syncdb again. Warning: you will lose all your data.

2: Manually update the column via SQL.

Type in python manage.py dbshell to get into your database shell and type in

ALTER TABLE my_table ALTER COLUMN my_column TYPE VARCHAR(200)

3: Learn and use a database migration tool like django south which will help keep your database updated with your model code.

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

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