Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form/database error: value too long for type character varying(4)

I'm trying to save a stripe (the billing service) company id [around 200 characters or so] to my database in Django.

The specific error is:

database error: value too long for type character varying(4)

How can I enable Django to allow for longer values?

I saw: value too long for type character varying(N) and: Django fixture fails, stating "DatabaseError: value too long for type character varying(50)"

  • my database is already encoded for UTF-8, according to my webhost.

  • EDIT : I see that one answer recommends making the column wider. Does that involve modifying the PostgreSQL database?

My specific system is Webfaction, CentOs shared machine, Django running on PostgreSQL. I would really appreciate a conceptual overview of what's going on and how I can fix it.

like image 446
zallarak Avatar asked Dec 13 '11 05:12

zallarak


People also ask

What is character varying in PostgreSQL?

The PostgreSQL Varchar data type is used to store characters of indefinite length based on the parameter n. It can store a string up to 65,535 bytes long. In the PostgreSQL Varchar data type i. e. Varchar(n), n is used to denote the character length limit.

What is the difference between varchar and text in PostgreSQL?

Difference Between PostgreSQL TEXT and VARCHAR Data Types The only difference between TEXT and VARCHAR(n) is that you can limit the maximum length of a VARCHAR column, for example, VARCHAR(255) does not allow inserting a string more than 255 characters long.

How do I alter a column in PostgreSQL?

First, specify the name of the table to which the column you want to change belongs in the ALTER TABLE clause. Second, give the name of column whose data type will be changed in the ALTER COLUMN clause. Third, provide the new data type for the column after the TYPE keyword.


1 Answers

Yes, make the column wider. The error message is quite clear: your 200 characters are too big to fit in a varchar(4).

First, update your model fields max_length attribute from 4 to a number that you expect will be long enough to contain the data you're feeding it.

Next up you have to update the database column itself as django will not automatically update existing columns.

Here are a few options:

1: Drop the database and run syncdb again. Warning: you will lose all your data.

2: Manually update the column via SQL:

Type in python manage.py dbshell to get into your database shell and type in

ALTER TABLE my_table ALTER COLUMN my_column TYPE VARCHAR(200)

3: Learn and use a database migration tool like django south which will help keep your database updated with your model code.

like image 128
Yuji 'Tomita' Tomita Avatar answered Oct 21 '22 13:10

Yuji 'Tomita' Tomita