10

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.

5
  • 3
    Yes, make the column wider. Type in python manage.py dbshell to get into your database shell. You can manually modify the table with ALTER TABLE statements, drop the database and run syncdb again, or learn and use a database migration tool like South Commented Dec 13, 2011 at 5:33
  • 1
    @YujiTomita I think thats a full enough response to be an answer imho. Commented Dec 13, 2011 at 5:34
  • Can you post the model you're trying to save to, or are you using someone else's code? You usually set the varchar length as a max_length parameter to the CharField in question. Commented Dec 13, 2011 at 5:37
  • 1
    @JamesKhoury Now I had to post an answer, and it took more work! Commented Dec 13, 2011 at 5:42
  • @YujiTomita Thanks for your help. I am going to try this today. Commented Dec 13, 2011 at 18:18

3 Answers 3

22

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I would +1 this just for the comment "you have to update the database column itself " thats something i often trip up on.
@James Khoury - this answer was your fault! And I thought I could get away with a comment and be on my way. : )
2

JUST IN CASE SOMEONE RUNS INTO THIS ERROR FOR DJANGO 3 and POSTGRES.

STEP 1: Go to your migration folder.

STEP 2: Navigate to your recent migration file.

STEP 3: Check for the operations list in the migration file.

STEP 4: Update the field or the table column max_length to the higher number to accommodate your data.

Comments

1

Using Django 1.11 and Postgres 9.6 I ran into this, for no apparent reason.

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

manage.py migrate

Then I set the correct length on the Model's max_length and ran another makemigrations and the ran migrate again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.