DatabaseError: value too long for type character varying(100)

后端 未结 11 462
北恋
北恋 2021-01-31 02:37

I have a Django web site running a mini CMS we\'ve built internally years ago, it\'s using postgresql. When saving a simple title and a paragraph of text I get the following err

相关标签:
11条回答
  • 2021-01-31 03:04

    First, try setting max_length to something reasonable on all applicable field types in your model.

    For example: MyText = models.CharField(max_length=2000)

    If you don't set a max_length, your database might be applying a default max_length, shorter than the length of your input data, causing your value too long for type character error.

    If that doesn't work and you started in SQLite and changed databases to PostgreSQL, the previous migrations from SQLite might be interfering with the new PostgreSQL migrations.

    Go into the migrations folder for your project and delete the old migration files to get a fresh start. Then try makemigrations and migrate again :)

    0 讨论(0)
  • 2021-01-31 03:05

    If it's not SlugField, FileField, or any other field mentioned here--scroll back to where the migration got stuck in the terminal. For me it was AddField

    Good talk.

    0 讨论(0)
  • 2021-01-31 03:06

    This is an error message from Postgres and not django.

    You seem to have changed the length of the field in the models.py, but that doesn't change the database length which was created when you did a manage.py syncdb.

    You have to alter the length of the field in the database, directly.

    0 讨论(0)
  • 2021-01-31 03:07

    I can bet money you have a models.SlugField without length set. The default length is 50 characters, most likely it's not enough for your use case.

    Change it to models.SlugField(max_length=255) and migrate your database schema.

    0 讨论(0)
  • 2021-01-31 03:07

    I had a similar problem with django-autoslugfield I was using a similar package and then switched over to django-autoslugfield

    I was getting this error: value too long for type character varying(50)

    despite the fact that my models.py had:

    slug = AutoSlugField(max_length=255, populate_from='name', unique=True)

    and in my db the it the type was character varying 255

    once i remove max_length=255 from the field i.e.

    slug = AutoSlugField(populate_from='name', unique=True)

    then it worked fine

    0 讨论(0)
  • 2021-01-31 03:09

    Michael Samoylov's answer pointed me in the right direction. I had the same error come up, except it was with a FileField.

    Fields have a max_length, even if you have not explicitly set a max_length. Increase the value so that your data fits to avoid the error.

    In my case, the data was too large to reasonably store in the database. I resorted to saving my file to disk, then saving the file path in the database.

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