我在我的 django Rest 项目中使用了 postgresql。 但是当我想迁移我的项目时,我遇到了一些字符变化的错误,如下所示
(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)
这是我的 showmigrations 命令结果:
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
这是我的帖子模型,我猜这会导致这个问题:
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}'
出了什么问题?我无法使用 postgresql 迁移我的 drf 项目
我尝试了很多方法,例如更改字符字段的 max_length 或将它们更改为文本字段。
我解决了, 您需要的所有事情就是删除 django 应用程序的应用程序中的 migrations dir 的内容除了“init.py”。
并删除您首先创建的数据库,然后再次创建它并授予目标用户。
然后运行此命令再次创建迁移:
python manage.py makemigrations
最后迁移项目。
python manage.py migrate
;)
注意:请注意,您首先创建的数据库是一个新数据库,并且没有任何数据。这种方法适用于新项目和新的 postgres 数据库