postgresql Django 表单/数据库错误:类型字符变化的值太长 (4)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8484689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:28:22  来源:igfitidea点击:

Django form/database error: value too long for type character varying(4)

pythondatabasedjangopostgresql

提问by zallarak

I'm trying to save a stripe (the billing service) company id [around 200 characters or so] to my database in Django.

我正在尝试将条带(计费服务)公司 ID [大约 200 个字符左右] 保存到我在 Django 中的数据库中。

The specific error is:

具体错误是:

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

How can I enable Django to allow for longer values?

如何启用 Django 以允许更长的值?

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

我看到: 类型字符变化的值太长(N)和: Django夹具失败,说明“数据库错误:类型字符变化的值太长(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?

  • 根据我的网络主机的说法,我的数据库已经编码为 UTF-8。

  • 编辑:我看到一个答案建议将列加宽。这是否涉及修改 PostgreSQL 数据库?

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.

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

回答by Yuji 'Tomita' Tomita

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

是的,使列更宽。错误消息非常清楚:您的 200 个字符太大而无法放入 varchar(4)。

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

首先,将您的模型字段max_length属性从 4更新为您期望足够长以包含您提供给它的数据的数字。

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

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

Here are a few options:

这里有几个选项:

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

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

2: Manually update the column via SQL.

2:通过SQL手动更新列。

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

输入python manage.py dbshell以进入您的数据库外壳并输入

ALTER TABLE my_table ALTER COLUMN my_column TYPE VARCHAR(200)

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

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

回答by chuck

Using Django 1.11 and Postgres 9.6 I ran into this, for no apparent reason.

使用 Django 1.11 和 Postgres 9.6 我遇到了这个,没有明显的原因。

I set max_length=255in the migration file and executed:

我在迁移文件中设置max_length=255并执行:

manage.py migrate

manage.py 迁移

Then I set the correct length on the Model's max_lengthand ran another makemigrationsand the ran migrateagain.

然后我在模型的max_length上设置了正确的长度并运行了另一个makemigrations并且再次运行了迁移