Django 和 PostgreSQL - 值对于类型字符变化来说太长(512)

发布于 2024-12-25 22:08:34 字数 1042 浏览 0 评论 0原文

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

我有一个插入到数据库中的示例对象,它在 SQLite 上工作,但在 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:

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

但该字符串不可能超过 512 字节。

我读过之前关于此错误的文章,其中一篇涉及编码。看来情况并非如此。

我在 Webfaction 上托管。我创建了一个使用 utf-8 编码的数据库,并继续使用syncdb。 Syncdb 工作完美,但现在该对象插入失败。

有人可以提供一些意见吗?谢谢。

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

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

Code snippet is:

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()

I am getting a:

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

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

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开浅夏 2025-01-01 22:08:34

经过一番挖掘 Django 文档

字符字段

使用 VARCHAR 列类型存储的任何字段都有其
max_length 限制为 255 个字符(如果您使用unique=True
对于该领域。

强调我的。您的字段有 unique=True 吗?
这是 Django 的限制,PostgreSQL 不会介意。您可能想要切换到数据类型 text< /a>. Django 术语中的 TextField


旧想法:

user 是一个 PostgreSQL 和任何 SQL 标准中的保留字。不要将其用作列名称。

如果您将其用双引号括起来,您可以使用它,但请远离这种愚蠢的行为。只是不要使用标识符的保留字。曾经。

另外...

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

motor_trendcars 没有像其他值一样被引用的任何特殊原因吗?外键,就像 @Ignacio 评论的那样?

After some digging in the Django documentation:

Character fields

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.

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.


Old ideas:

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.

Also ...

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文