Value too long for character like (100) ---- newly connected database, did nothing in db

I recently switched to postgresql, I thought everything was working fine until I realized when I was creating the post. I get too much value for a variable of type (100). Now I looked at it on Google and saw some similar questions, but when I tried some solutions, none of them worked. I will explain why my question is different. I have this code in models.py

class Post(models.Model): url = models.URLField(max_length=250, blank=True, null=True) slug = models.CharField(max_length=255, unique=True) objects = models.Manager() @property def save(self, *args, **kwargs): self.slug = uuslug(self.title, instance=self, max_length=255) super(Post, self).save(*args, **kwargs) 

As I saw some recommended solutions, I tried changing my max_length to 100. I have no idea why this is happening, I have nothing in db. I recently switched to postgresql. Can you help me why this error occurs and how can I fix it? should i move away from uuslug?

Full model

 . class Category(models.Model): name = models.CharField(max_length=128, unique=True) description = models.TextField(verbose_name=('describe')) author = models.ForeignKey(settings.AUTH_USER_MODEL) def __unicode__(self): return self.name def get_absolute_url(self): return "/category/%s/" %self.name def my_handler(sender, instance, created, **kwargs): action.send(instance.author, verb='following', target=Category) post_save.connect(my_handler, sender=Category) class Post(models.Model): category = models.ForeignKey(Category, verbose_name=('community')) pub_date = models.DateTimeField(auto_now_add = True) url = models.URLField(max_length=250, blank=True, null=True) video = EmbedVideoField(verbose_name='link',help_text="Youtube", blank=True, null=True) title = models.CharField(max_length = 50) moderator = models.ForeignKey(User) views = models.IntegerField(default=0) slug = models.CharField(max_length=255, unique=True) objects = models.Manager() # default manager content = RichTextUploadingField(config_name='default') rank_score = models.FloatField(default= 1) image = models.ImageField(upload_to='images',blank=True, null=True) thumbnail = models.ImageField(upload_to='images', blank=True, null=True) @property def domain(self): long_url = urlparse(self.url).netloc if self.url else "be kind to one another" return long_url.split('.', 1)[1] if long_url.split('.', 1)[0] == 'www' else long_url def save(self, *args, **kwargs): self.slug = uuslug(self.title, instance=self, max_length=255) super(Post, self).save(*args, **kwargs) def __unicode__(self): return self.title 

Here is the full traceback

T

 raceback: File "env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 132. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view 71. return self.dispatch(request, *args, **kwargs) File "env/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper 34. return bound_func(*args, **kwargs) File "env/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 22. return view_func(request, *args, **kwargs) File "env/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func 30. return func.__get__(self, type(self))(*args2, **kwargs2) File "ebagu/main/views.py" in dispatch 191. return super(PostCreateView, self).dispatch(request, *args, **kwargs) File "env/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch 89. return handler(request, *args, **kwargs) File "env/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post 249. return super(BaseCreateView, self).post(request, *args, **kwargs) File "env/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post 215. return self.form_valid(form) File "ebagu/main/views.py" in form_valid 186. self.object.save() File "ebagu/main/models.py" in save 66. super(Post, self).save(*args, **kwargs) File "env/local/lib/python2.7/site-packages/django/db/models/base.py" in save 734. force_update=force_update, update_fields=update_fields) File "env/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base 762. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "env/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table 846. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "env/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_insert 885. using=using, raw=raw) File "env/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method 127. return getattr(self.get_queryset(), name)(*args, **kwargs) File "env/local/lib/python2.7/site-packages/django/db/models/query.py" in _insert 920. return query.get_compiler(using=using).execute_sql(return_id) File "env/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql 974. cursor.execute(sql, params) File "env/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute 79. return super(CursorDebugWrapper, self).execute(sql, params) File "env/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute 64. return self.cursor.execute(sql, params) File "env/local/lib/python2.7/site-packages/django/db/utils.py" in __exit__ 97. six.reraise(dj_exc_type, dj_exc_value, traceback) File "env/local/lib/python2.7/site-packages/django/db/backends/utils.py" in execute 64. return self.cursor.execute(sql, params) Exception Type: DataError at /add_post/ Exception Value: value too long for type character varying(100) 
+7
django postgresql
source share
2 answers

I do not think that you need help in fixing this problem, since you need help in debugging it. Once the problem becomes clear, the solution seems clear. Traceback may be a little obscure because it goes through so much Django source code, and that doesn't tell you which of your fields has a problem.

Background for this problem

For starters, we are having trouble saving a Post instance. Well, look at all of these fields that you have in your model definition:

  ... url = models.URLField(max_length=250, blank=True, null=True) video = EmbedVideoField(verbose_name='link',help_text="Youtube", blank=True, null=True) content = RichTextUploadingField(config_name='default') image = models.ImageField(upload_to='images',blank=True, null=True) thumbnail = models.ImageField(upload_to='images', blank=True, null=True) 

They may not look like text fields, but many of them are variations of text fields, because if you think about it, you are probably not going to store entire files in your database. Instead, you do (and what Django does by default), save the file somewhere on some drive, and then in the database you save the path to that file so that you can get it when you need to.

In addition, this is probably a waste for storing path files in db like LongText or something else, so each FileField we have fields with max_length whether we indicate it or not. Thus, all of the above fields have an implicit max_length . You can find this by reading the source code for Django.

Sample Sources

I never used EmbedVideoField , but it turns out to be a subclass of models.URLField , which means it has max_length set to 200 by default, unless you specify it.

In addition, your various ImageField are simply subclasses of FileField , which has a default max_length of 100 .

How to debug problems like this in the future?

Now this will not help us find out which of your fields produces an error in this case. For this, I would probably set a breakpoint somewhere in the code, perhaps here:

 File "ebagu/main/models.py" in save 66. super(Post, self).save(*args, **kwargs) 

By setting a breakpoint, I mean the following:

Go to line 65 in the ebagu/main/models.py module mentioned above and enter the following and save the module: import pdb; pdb.set_trace() import pdb; pdb.set_trace()

(I really prefer ipdb myself, but that requires Ipython, and I also have a strong preference ...)

Start the local server and follow the steps that caused this problem. At the end, you will submit your form, and if you look at the console on which you launched your server, you will eventually be dumped into the shell directly on line 65. This is a pdb shell that has different rules from a regular shell, but you can evaluate the Post instance that will be stored in the future by looking at the various fields of the instance itself, self and running Python code in the context of calling this method:

 (pdb) len(self.image.path) 

Using this, I would manually evaluate the various fields and see which one has this very long record that suffocates the save (probably one of your ImageField s).

Warning solution

Alternatively, you can simply add max_length to all of these, but be warned that you will most likely need to perform database migrations for any limited text field that you change, since your database is still checking the input length for how the column is determined. fooobar.com/questions/704551 / ....

Footnote

Why didn't this appear before you switched to Postgresql? There are many potential reasons, but this is probably due to how the previous database was installed and how the Postgresql database was created (manually or Django migration?).

Perhaps this is also related to whether you have changed where these things are stored. Have you changed the MEDIA settings so that the paths in which the files are stored become much longer?

What you really have to do is look directly at your database. Open a psql instance and ask it to describe your tables for you. It will tell you which fields are limited to 100 characters, and these are the fields that give you problems.

+7
source share

Look into the database.


Quick psql tutorial

Launch the postgres client shell, for example. with python manage.py dbshell . Something like this shows:

 ebagu=> 

Lines

 File "ebagu/main/models.py" in save 66. super(Post, self).save(*args, **kwargs) 

show that you are trying to keep the Post model in what I am assuming is app main . If you have not changed the table in Post Meta, the table should be something like main_post .

You want to display all the tables in the postgres shell with the command:

 \d 

He will list the tables in the same way:

  List of relations Schema | Name | Type | Owner --------+-----------------------------------+---------------+-------- public | django_migrations | table | hruske public | django_migrations_id_seq | sequence | hruske public | django_session | table | hruske 

If you want to check the details of the django_session table, you can do:

 \d django_session 

And you will get something like this:

  Table "public.django_session" Column | Type | Modifiers --------------+--------------------------+----------- session_key | character varying(40) | not null session_data | text | not null expire_date | timestamp with time zone | not null Indexes: "django_session_pkey" PRIMARY KEY, btree (session_key) "django_session_de54fa62" btree (expire_date) "django_session_session_key_461cfeaa630ca218_like" btree (session_key varchar_pattern_ops) 

Here you can see which fields are defined as varchar(100) .

If you want to know more psql commands, type \?

+1
source share

All Articles