I'm trying to deploy my Django application to Heroku and I need to migrate a database to create tables, but it's giving me an error, I understand that it's an error with fields and max_length, but I don't have any fields with (1) max length:
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(1)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, 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 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 245, 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 227, 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 106, in database_forwards
field,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 487, in add_field
self.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 142, in execute
cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 66, 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 75, 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 84, in _execute
return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 90, 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 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(1)
My models don't have any fields with max_length=1, it's all working locally on my machine with SQLite but on Postgres, it gives an error:
from django.db import models
from django.contrib.auth.models import User
class Product(models.Model):
name = models.CharField(max_length=100)
calories = models.IntegerField()
price = models.DecimalField(max_digits=8, decimal_places=2)
def __str__(self):
return f'id:{self.id}, {self.name}, price:{self.price}, cals:{self.calories}'
class RecipeItem(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True)
grams = models.IntegerField(null=True, blank=True)
@property
def get_total(self):
total = (self.product.price / 100) * self.grams
return total
def __str__(self):
return f'id:{self.id}'
class Recipe(models.Model):
name = models.CharField(max_length=100)
description = models.TextField(max_length=1000)
items = models.ManyToManyField(RecipeItem, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
@property
def get_total_price(self):
recipeitems = self.items.all()
total = sum([item.get_total for item in recipeitems])
return total
@property
def get_total_calories(self):
recipeitems = self.items.all()
total = int(sum([(item.product.calories / 100) * item.grams for item in recipeitems]))
return total
def get_products(self):
return "
".join([p.products for p in self.product.all()])
def __str__(self):
return f'id:{self.id}, {self.name}, price:{self.get_total_price}, cals:{self.get_total_calories}'
class Plan(models.Model):
PLAN_DAYS_CHOICES = [
('3d', '3-day plan'),
('7d', '7-day plan'),
]
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
name = models.CharField(max_length=255, blank=True, null=True)
type = models.CharField(max_length=255, choices=PLAN_DAYS_CHOICES)
date_created = models.DateTimeField(auto_now_add=True, max_length=255)
@property
def get_total_items(self):
planitems = self.planitem_set.all()
total = planitems.count()
return total
@property
def get_total_price(self):
planitems = self.planitem_set.all()
total = sum([item.recipe.get_total_price for item in planitems])
return total
@property
def get_total_calories(self):
planitems = self.planitem_set.all()
total = sum([item.recipe.get_total_calories for item in planitems])
return total
def __str__(self):
return f'id:{self.id}, {self.type}, owner:{self.user.username}'
class PlanItem(models.Model):
DAYS_CHOICES = [
('1', 'Day 1'),
('2', 'Day 2'),
('3', 'Day 3'),
('4', 'Day 4'),
('5', 'Day 5'),
('6', 'Day 6'),
('7', 'Day 7'),
]
TIME_CHOICES = [
('br', 'Breakfast'),
('ln', 'Lunch'),
('dn', 'Dinner'),
('sn', 'Snack'),
]
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE, blank=True, null=True)
plan = models.ForeignKey(Plan, on_delete=models.CASCADE, blank=True, null=True)
day = models.CharField(max_length=255, choices=DAYS_CHOICES)
time = models.CharField(max_length=255, choices=TIME_CHOICES, null=True, blank=True)
def __str__(self):
return f'id:{self.id} recipe under plan id:{self.plan.id}'
question from:
https://stackoverflow.com/questions/65932819/django-db-utils-dataerror-value-too-long-for-type-character-varying1