psycopg2.errors.StringDataRightTruncation:值太长,无法用于类型字符变化(2)Django

问题描述 投票:0回答:1

[我正在Heroku上部署Django网站,并在迁移时遇到此错误。

我的网站已启动并在localhost上运行,但在部署时遇到此错误。

这是我的模特-

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import pre_save
from Dipesh_Pal.utils import unique_slug_generator


# Create your models here.
class Home(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(max_length=255, null=True, blank=True)
    body = models.TextField()
    CATEGORY_CHOICES = [
        ('NEWS', 'News'),
        ('ANDROID', 'Android'),
        ('PC', 'PC'),
        ('OTHERS', 'Others'),
    ]
    category = models.CharField(
        max_length=20,
        choices=CATEGORY_CHOICES,
        default='OTHERS',
    )

    link = models.URLField(blank=True)
    date = models.DateTimeField(auto_now_add=True)
    pub_date = models.DateTimeField(auto_now_add=True)
    thumb = models.ImageField(default='default.png', blank=True)
    author = models.ForeignKey(User, default=None, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def snippet(self):
        return self.body[:100]+'...'

    def get_absolute_url(self):
        return '/' + self.title


def slug_generator(sender, instance, *args, **kwargs):
    if not instance.slug:
        instance.slug = unique_slug_generator(instance)


pre_save.connect(slug_generator, sender=Home)

我在迁移时遇到此错误-

python manage.py migrate
You may have some problem with YouTube
Instagram Followers:  341
You may have some problem with Twitter
Operations to perform:
  Apply all migrations: accounts, admin, auth, contenttypes, home, sessions, sites
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying accounts.0001_initial... OK
  Applying accounts.0002_auto_20190226_1249... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying home.0001_initial... OK
  Applying home.0002_home_thumb... OK
  Applying home.0003_home_author... OK
  Applying home.0004_home_trip_number... OK
  Applying home.0005_auto_20190104_0239... OK
  Applying home.0006_auto_20190104_0239... OK
  Applying home.0007_auto_20190104_0242... OK
  Applying home.0008_auto_20190104_0244... OK
  Applying home.0009_remove_home_link... OK
  Applying home.0010_home_link... OK
  Applying home.0011_auto_20190105_1138... OK
  Applying home.0012_auto_20190226_1249... OK
  Applying home.0013_auto_20190228_1615... OK
  Applying home.0014_auto_20190306_1623... OK
  Applying home.0015_auto_20190518_1502...Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(2)


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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle    fake_initial=fake_initial,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
    field,
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 435, in add_field
    self.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 133, in execute
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers    return executor(sql, params, many, context)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(2)

我已经尝试更改max_length = 100或max_length = 255,但对我不起作用。

我正在heroku上部署此应用程序,这里是我的github项目“ https://github.com/Dipeshpal/Dipesh-Pal-Django_Website”,并且我正在使用此方法来部署Web应用程序https://youtu.be/MoX36izzEWY

[请帮助我解决此问题,我停留在部署阶段。

python django heroku django-models django-migrations
1个回答
0
投票

在您的/ migrations / versions文件夹中,打开最新的.py文件。它应该有长度调整。类似于:

sa.Column('body', sa.String(length=2), nullable=True),

将2更改为所需值,然后保存,然后再次上传到Heroku。请注意,根据您的情况,该行中的其他项目与上面的有所不同。

© www.soinside.com 2019 - 2024. All rights reserved.