Search code examples
pythondjangodjango-rest-frameworkfactory-boy

value too long for type character Django Factory


I have below OrderDataFactory class.

class OrderDataFactory(factory.django.DjangoModelFactory):

    class Meta:
        model = models.OrderData

    order = factory.SubFactory(OrderFactory)
    category = 'single',
    quantity = 75.6
    price_per_kg = 10.5
    sku = factory.SelfAttribute('crop_data.id')

    class Params:
        crop_data = factory.SubFactory(CropFactory)

models.py

class OrderData(models.Model):
    CATEGORY_CHOICES = (
        (SKUCategory.SINGLE, 'Single Sku'),
        (SKUCategory.COMBO, 'Combo Sku'),
    )
    sku = models.PositiveIntegerField(null=False, validators=[MinValueValidator(1)])
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    quantity = models.FloatField()
    price_per_kg = models.FloatField()
    category = models.CharField(choices=CATEGORY_CHOICES, max_length=8)
    class Meta:
        unique_together = (('sku', 'order', 'category'),)

Category.py

class SKUCategory:
    SINGLE = 'single'
    COMBO = 'combo'

i am getting following error.

django.db.utils.DataError: value too long for type character varying(8)

even max length for category is 6.


Solution

  • You have a comma at the end of this line category = 'single',. This is defining category as the tuple ('single',) which is probably being converted to the string that is 11 characters long. Remove the comma to use a string

    category = 'single'