6 django postgresql psycopg2 django-database
我正在从测试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()
Run Code Online (Sandbox Code Playgroud)
我得到一个:
DatabaseError at /create/
value too long for type character varying(512)
Traceback
(...)
description = 'GORGEOUS ORANGE SS!!',
(...)
Run Code Online (Sandbox Code Playgroud)
我的模型的描述字段有512个最大字符长度:
description = models.CharField(max_length=512,default='')
Run Code Online (Sandbox Code Playgroud)
但是字符串不能超过512字节.
我之前读过有关此错误的帖子,其中一篇涉及编码.似乎并非如此.
我在Webfaction上托管.我创建了一个使用utf-8编码的数据库,然后继续使用syncdb.Syncdb工作正常但现在这个对象插入失败了.
有人可以提供一些意见吗?谢谢.
经过一番深入研究Django 文档后:
字符字段
如果您对 字段使用unique=True,则使用 VARCHAR 列类型存储的任何字段的 max_length 限制为 255 个字符。
强调我的。你有unique=True野外用的吗?这是 Django 的限制,PostgreSQL 不会介意。您可能想切换到数据类型text。TextField用姜戈的话说。
旧观念:
user是PostgreSQL和任何 SQL 标准中的保留字。不要将其用作列名称。
如果您将其用双引号引起来,您可以使用它,但请远离这种愚蠢的行为。只是不要使用标识符的保留字。曾经。
还 ...
user = motor_trend,
name = 'Camaro 2010',
category = cars,
Run Code Online (Sandbox Code Playgroud)
有什么特殊原因为什么motor_trend和cars不像其他值一样被引用吗?外键,就像 @Ignacio 评论的那样?