Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

python - DatabaseError: value too long for type character varying(20)

Solution:- I had deleted my migration .py files which caused this problem. Creating a new app with the same code works now.

I am not sure why i get a varying(20) error even when the max_length I've set is 200 for a CharField I looked at all the other posts and all of them had an issue with the length of the value. However, even if i enter "abc" in the name field it gives me this error. Any help is appreciated. Thanks!

Django Version: 3.1.5 Python Version: 3.8.5

from django.db import models
from django.contrib.gis.db import models
from django.db.models import Manager as GeoManager

# Create your models here.
class Incidences(models.Model):
    name = models.CharField(max_length=200)
    location = models.PointField(srid=4326)
    objects = GeoManager()
    
    def _str_(self):
        return self.name
Traceback (most recent call last):
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbackendsutils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

The above exception (value too long for type character varying(20)
) was the direct cause of the following exception:
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangocorehandlersexception.py", line 47, in inner
    response = get_response(request)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangocorehandlersase.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangocontribadminoptions.py", line 614, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangoutilsdecorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangoviewsdecoratorscache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangocontribadminsites.py", line 233, in inner
    return view(request, *args, **kwargs)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangocontribadminoptions.py", line 1653, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangoutilsdecorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangoutilsdecorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangocontribadminoptions.py", line 1534, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangocontribadminoptions.py", line 1580, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangocontribadminoptions.py", line 1093, in save_model
    obj.save()
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbmodelsase.py", line 753, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbmodelsase.py", line 790, in save_base
    updated = self._save_table(
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbmodelsase.py", line 895, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbmodelsase.py", line 933, in _do_insert
    return manager._insert(
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbmodelsmanager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbmodelsquery.py", line 1254, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbmodelssqlcompiler.py", line 1397, in execute_sql
    cursor.execute(sql, params)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbackendsutils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbackendsutils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbackendsutils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbackendsutils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbutils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:Usersanujgeo_django_tutorialutorial_geodjangolifeingislibsite-packagesdjangodbackendsutils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

Exception Type: DataError at /admin/reporter/incidences/add/
Exception Value: value too long for type character varying(20)
question from:https://stackoverflow.com/questions/65947889/databaseerror-value-too-long-for-type-character-varying20

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...