Django and PostgreSQL - value too long for type character varying(512)

偶尔善良 提交于 2019-12-12 10:48:31

问题


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.


回答1:


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?



来源:https://stackoverflow.com/questions/8767543/django-and-postgresql-value-too-long-for-type-character-varying512

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!