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

I am trying to store the stripe (billing service) company identifier [about 200 characters or so] in my database in Django.

Specific error:

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

How to enable Django for longer values?

I saw: a value is too long for a character of type (N) and also: Django crashes, indicating "DatabaseError: too long a value for character of type (50)"

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

  • EDIT . I see that one answer recommends making the column wider. Does this change to a PostgreSQL database?

My specific system is Webfaction, a common CentOs machine, Django runs on PostgreSQL. I would really appreciate a conceptual review of what is happening and how I can fix it.

+4
python database django postgresql
Dec 13 '11 at 5:22
source share
2 answers

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

First, update the attributes of your max_length field from 4 to a number that you expect to be long enough to contain the data you feed it.

Then you need to update the database column as django will not automatically update existing columns .

Here are a few options:

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

2: manually update the column through SQL.

Type python manage.py dbshell to enter your database shell and type

 ALTER TABLE my_table ALTER COLUMN my_column TYPE VARCHAR(200) 

3: Examine and apply a database migration tool such as django south , which will help update the database using model code.

+14
Dec 13 2018-11-11T00:
source share

Using Django 1.11 and Postgres 9.6, I came across this for no apparent reason.

I set max_length = 255 to the migration file and executed:

manage.py migrate

Then I set the correct length to Model max_length and performed another makemigrations, and the escape migrated again.

+1
Oct 03 '17 at 13:14
source share



All Articles