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

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

问题描述

我想要做的是将Django中的数据库(计费服务)公司ID(大约200个字符左右)保存到我的数据库中。如何启用Django以允许更长的值?

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?

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

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

我看到:
类型字符变化的值太长(N )
和:
Django fixture失败,表示DatabaseError:值太长,类型字符变化(50)


    <根据我的webhost,我的数据库已经被编码为UTF-8。
    编辑 - 我看到一个答案建议使列更宽。这是否涉及修改PostgreSQL数据库?
  • 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?

我的具体系统是Webfaction,CentOs共享机器,运行在PostgreSQL上的Django。我真的很感激一下关于发生了什么的概念性概述,以及如何解决它。

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.

推荐答案

是的,使列更宽。错误信息很清楚:您的200个字符太大,无法适应varchar(4)。

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

首先,将您的模型字段从$ 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.

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

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

以下是几个选项:

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

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

2:通过SQL手动更新列。

2: Manually update the column via SQL.

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

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:学习和使用数据库迁移工具如 django south ,这将有助于使您的数据库更新您的模型代码。

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天全站免登陆