Django和PostgreSQL-类型字符变化的值太长(512) [英] Django and PostgreSQL - value too long for type character varying(512)

查看:212
本文介绍了Django和PostgreSQL-类型字符变化的值太长(512)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从测试的SQLite数据库迁移到PostgreSQL数据库。

I am migrating from a test SQLite database to a PostgreSQL database.

我有一个插入到数据库中的示例对象,该对象在SQLite上可用,但是我在PostgreSQL中出错。

I have a sample object that is inserted in the database, that worked on SQLite but is giving me an error in PostgreSQL.

代码段是:

car = CarItem.objects.create(
    user = motor_trend,
    name = 'Camaro 2010',
    category = cars,
    condition = 'Used',
    price = '28,547.00',
    production_year = '2010',
    color_interior = 'Black',
    color_exterior = 'Inferno Orange Metallic',
    reference = 'PRC17288',
    location_of_creation = 'Undisclosed',
    location_current = 'Columbus, OH, USA',
    description = 'GORGEOUS ORANGE SS!!',
)
car.save()

我正在获取:

DatabaseError at /create/
value too long for type character varying(512)

Traceback
(...)
    description = 'GORGEOUS ORANGE SS!!',
(...)

我的模型的说明字段的最大值为512字符长度:

The description field of my model has a 512 max char length:

description = models.CharField(max_length=512,default='')

但是字符串不能超过512个字节。

But there is no way that string is over 512 bytes.

我有阅读有关此错误的先前文章,其中一篇涉及编码。似乎并非如此。

I have read previous posts about this error, one referring to the encoding. Does not appear to be the case.

我托管在Webfaction上。我创建了一个使用utf-8编码的数据库,然后继续使用syncdb。 Syncdb可以正常工作,但是现在此对象插入失败。

I am hosted on Webfaction. I created a database, with utf-8 encoding, and proceeded to use syncdb. Syncdb worked perfectly but now this object insertion fails.

有人可以提供一些输入吗?谢谢。

Can somebody give some input? Thank you.

推荐答案

经过 Django文档


字符字段

Character fields

任何使用VARCHAR列类型存储的字段的
max_length 限制为255个字符(如果您使用的是 unique = True

Any fields that are stored with VARCHAR column types have their max_length restricted to 255 characters if you are using unique=True for the field.

强调矿井。您是否有 unique = True 字段?
这是Django的限制,PostgreSQL不在乎。您可能要切换到数据类型 text TextField

Emphasis mine. Do you have unique=True for the field? This is a Django restriction, PostgreSQL wouldn't mind. You might want to switch to data type text. TextField in Django parlance.

旧思路:

用户 保留在PostgreSQL中的单词和任何SQL标准。

user is a reserved word in PostgreSQL and any SQL standard. Don't use it as column name.

如果用双引号将其括起来,则可以使用它,但是请不要使用它。 。只是不要使用保留字作为标识符。

You could use it, if you enclosed it in double quotes, but stay away from that folly. Just don't use reserved words for identifiers. Ever.

也...

user = motor_trend,
name = 'Camaro 2010',
category = cars,

任何特殊原因为什么 motor_trend cars 没有像其他值一样被引用?像@Ignacio这样的外键有评论吗?

Any particular reason why motor_trend and cars are not quoted like the other values? Foreign keys, like @Ignacio commented?

这篇关于Django和PostgreSQL-类型字符变化的值太长(512)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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