Django 和 PostgreSQL - 值对于类型字符变化来说太长(512)
我正在从测试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番挖掘 Django 文档:
强调我的。您的字段有
unique=True
吗?这是 Django 的限制,PostgreSQL 不会介意。您可能想要切换到数据类型
text
< /a>. Django 术语中的TextField
。旧想法:
user
是一个 PostgreSQL 和任何 SQL 标准中的保留字。不要将其用作列名称。如果您将其用双引号括起来,您可以使用它,但请远离这种愚蠢的行为。只是不要使用标识符的保留字。曾经。
另外...
motor_trend
和cars
没有像其他值一样被引用的任何特殊原因吗?外键,就像 @Ignacio 评论的那样?After some digging in the Django documentation:
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 ...
Any particular reason why
motor_trend
andcars
are not quoted like the other values? Foreign keys, like @Ignacio commented?