0

i used from postgresql for my django rest project. but when i want to migrate my project i got some error for character varying like below

(myenv) root@srv524469:~/django-paynans# python manage.py migrate
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, django_celery_beat, posts, rest_framework_tracking, sessions, store
Running migrations:
  Applying posts.0005_post_description...Traceback (most recent call last):
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(200)


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/django-paynans/manage.py", line 22, in <module>
    main()
  File "/root/django-paynans/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/core/management/base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 356, in handle
    post_migrate_state = executor.migrate(
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 135, in migrate
    state = self._migrate_all_forwards(
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
    state = self.apply_migration(
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/migrations/executor.py", line 252, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/migrations/migration.py", line 132, in apply
    operation.database_forwards(
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/migrations/operations/fields.py", line 108, in database_forwards
    schema_editor.add_field(
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 713, in add_field
    self.execute(sql, params)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/postgresql/schema.py", line 48, in execute
    return super().execute(sql, None)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/base/schema.py", line 201, in execute
    cursor.execute(sql, params)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/root/miniconda/envs/myenv/lib/python3.9/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
django.db.utils.DataError: value too long for type character varying(200)

and it is my showmigrations command resaults:

accounts
 [X] 0001_initial
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
django_celery_beat
 [X] 0001_initial
 [X] 0002_auto_20161118_0346
 [X] 0003_auto_20161209_0049
 [X] 0004_auto_20170221_0000
 [X] 0005_add_solarschedule_events_choices
 [X] 0006_auto_20180322_0932
 [X] 0007_auto_20180521_0826
 [X] 0008_auto_20180914_1922
 [X] 0006_auto_20180210_1226
 [X] 0006_periodictask_priority
 [X] 0009_periodictask_headers
 [X] 0010_auto_20190429_0326
 [X] 0011_auto_20190508_0153
 [X] 0012_periodictask_expire_seconds
 [X] 0013_auto_20200609_0727
 [X] 0014_remove_clockedschedule_enabled
 [X] 0015_edit_solarschedule_events_choices
 [X] 0016_alter_crontabschedule_timezone
 [X] 0017_alter_crontabschedule_month_of_year
 [X] 0018_improve_crontab_helptext
posts
 [X] 0001_initial
 [X] 0002_examplecategory_examplepost
 [X] 0003_comment
 [X] 0004_alter_examplepost_video_alter_post_video
 [ ] 0005_post_description
 [ ] 0006_questions
 [ ] 0007_remove_examplepost_video_examplepost_content_photo
 [ ] 0008_ticket
 [ ] 0009_alter_questions_question
 [ ] 0010_alter_examplecategory_description_and_more
 [ ] 0011_alter_post_description
 [ ] 0012_alter_post_description
 [ ] 0013_alter_post_cover_photo_alter_post_description
rest_framework_tracking
 [ ] 0001_initial
 [ ] 0002_auto_20170118_1713
 [ ] 0003_add_errors
 [ ] 0004_add_verbose_name
 [ ] 0005_auto_20171219_1537
 [ ] 0006_view_and_view_method_nullable
 [ ] 0006_auto_20180315_1442
 [ ] 0007_merge_20180419_1646
 [ ] 0008_auto_20200201_2048
 [ ] 0009_view_method_max_length_200
 [ ] 0010_auto_20200609_1404
 [ ] 0011_auto_20201117_2016
 [ ] 0012_auto_20210930_0713
 [ ] 0013_apirequestlog_user_agent
 [ ] 0014_alter_apirequestlog_id
sessions
 [ ] 0001_initial
store
 [ ] 0001_initial
 [ ] 0002_product_best_mark

and this is my post model that i guessing this make this problem:

class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    subject = models.ForeignKey(Subject, on_delete=models.SET_NULL, null=True)
    TYPE_CHOICES = (
        ('post', 'Post'),
        ('article', 'Article'),
    )
    post_type = models.CharField(max_length=20, choices=TYPE_CHOICES)
    title = models.CharField(max_length=50)
    description = models.CharField(max_length=190)
    content = models.TextField()
    cover_photo = models.ImageField(
        upload_to=cover_photo_path_generator
    )
    video = models.URLField(null=True, blank=True)
    required_time = models.IntegerField(default=10)
    saw_count = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def increment_saw_count(self):
        """The Increment the saw count of the post by 1."""
        Post.objects.filter(pk=self.pk).update(saw_count=F('saw_count') + 1)

    def increment_like(self):
        """Increment the like count of the post by 1."""
        Post.objects.filter(pk=self.pk).update(likes=F('likes') + 1)

    def __str__(self):
        return f'{self.title}_{self.pk}'

what is wrong? that i cant migrate my drf project with postgresql

i tried many ways like changing max_length of charfields or changing them to text field.

3
  • The issue is in posts.0005_post_description, what are you doing there? At a guess increasing description from 190 -> 200 characters and adding new description, one of which I assume is > 200 characters. Commented May 13, 2024 at 14:47
  • i changed this to 300 or 200 , and i changed to textfield too but i got error again. @AdrianKlaver Commented May 13, 2024 at 15:06
  • 1) Exact same error message? 2) You can take the error to be correct, you are trying to put too long a string into the field. I would look at the Postgres log to see the sequence events that take place when you do the migration. Best guess, what you think is happening is not actually happening. Commented May 13, 2024 at 15:30

1 Answer 1

0

I think I solved it, all of things that you need is removing the content of migrations dir in apps of django application except init py file.

and drop the database that you created first, and create it again and grant the target user.

and then run this command for creating migrations again:

python manage.py makemigrations

and in the end migrate the project.

python manage.py migrate

note: be aware that your database you are using first, be a new one and without any data. this approach is for new projects and new postgres databases

Sign up to request clarification or add additional context in comments.

3 Comments

Migrations support incremental upgrades. Unless you are creating an entirely new instance of a database you should not have to do the above. You might want to update your question to make it clearer what you where trying to achieve with the migration.
in debug mode for drf project. my database was sqlite3.and i making some migrations for this first. and then i using from postgresql for deploying my project in server. but the migrations didnt work for postgresql, while i was using from makemigration command and migrate command. but above error occured. but when i deleted migrations file in my django project and made them again, it worked and my postgrsql databases created successfully. i think the migration files had some problem. @AdrianKlaver
This in debug mode for drf project. my database was sqlite3. was your problem. Develop against the database you will use in production. Sqlite3 is a great embedded database, however it is not a client/server database like Postgres. Also it has significant behavioral differences from Postgres, especially when it comes to data typing. You where setting yourself for failure by using it for dev purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.