Django error: Value too long for type character varying(2)

烈酒焚心 提交于 2021-01-29 05:27:23

问题


I did a database flush to reset my database. I am getting this error when I try to do the following code.

Code that throws error:

    try:
        print("Attempting to load %s" % store.get('name'))
        # THIS NEXT LINE THROWS ERROR
        store_obj = Store.objects.get(name=store.get('name'))
    except Store.DoesNotExist:
        store_obj = Store(name=store.get('name'),
                          last_updated=last_updated,
                          address=store.get('address'),
                          city=store.get('city'),
                          state=store.get('state'),
                          zip_code=store.get('zip_code'))

Error:

Exception Type: DataError at /stores/
Exception Value: value too long for type character varying(2)

\d of stores_store

Model in django:

class Store(models.Model):

    name = models.CharField(max_length=200)
    description = models.TextField()
    created_at = models.DateTimeField(default=datetime.now, blank=True)
    last_updated = models.DateTimeField(default=datetime.now, blank=True)

    # Address
    address = models.CharField(_("address"), max_length=128)
    city = models.CharField(_("city"), max_length=128)
    state = USStateField(_("state"))
    zip_code = USZipCodeField(_("zip code"), max_length=5)

    def __str__(self):
        return self.name

As you can see the store name is a VARCHAR of 200, not 2. However, I cannot complete this get operation without getting this error. I tried another flush and makemigrations -> migrate but still have no luck. What else can I try?

Thanks!


回答1:


the column "state" has USStateField(_("state")) which I assume is varying(2), are you trying to add a tuple which state is bigger than varying(2)?

I can suggest you to change the USStateField(_("state")) to a CharField or to put a max_length.

If that doesn't work you can try to delete the migrations history to make them again. Inside the app folder in migrations delete all the files that look like 0001_initial.py, don't delete the init.py inside the migrations folder, hope it helps :D



来源:https://stackoverflow.com/questions/53113178/django-error-value-too-long-for-type-character-varying2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!