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

前端 未结 2 1779
忘了有多久
忘了有多久 2020-12-16 02:54

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:

databas         


        
相关标签:
2条回答
  • 2020-12-16 02:56

    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.

    0 讨论(0)
  • 2020-12-16 03:02

    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.

    0 讨论(0)
提交回复
热议问题