MySQL:django.db.utils.OperationalError:(1366,“不正确的整数值:'第1行'列'category_id'的'类别对象'”)

时间:2017-01-15 07:07:55

标签: python mysql django python-3.x

将MySQL与Django一起使用,我已经将模型从使用“category”的字符串更改为使用FK。它现在被

打破了
django.db.utils.OperationalError: (1366, "Incorrect integer value: 'Category object' for column 'category_id' at row 1")

最初看起来像:

class ItemRecord(models.Model):
    catalog_id = models.IntegerField()
    name = models.CharField(max_length=250)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    active = models.BooleanField(default=True) # is the item being sold at all? (carried)
    in_stock = models.BooleanField(default=True) # is the item currently in stock?
    banned = models.BooleanField(default=False)
    category = models.CharField(max_length=250, null=True, blank=True)

    class Meta:
        abstract = True


class FermentableRecord(ItemRecord):#record of each item
    pass

class HopRecord(ItemRecord):
    pass

class YeastRecord(ItemRecord):
    pass

然后,我使用FK:

class Category(models.Model):
    name = models.CharField(max_length=250)
    banned = models.BooleanField(default=False)


class ItemRecord(models.Model):
    catalog_id = models.IntegerField()
    name = models.CharField(max_length=250)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    active = models.BooleanField(default=True) # is the item being sold at all? (carried)
    in_stock = models.BooleanField(default=True) # is the item currently in stock?
    banned = models.BooleanField(default=False)
    category = models.ForeignKey(Category, related_name="items")

    class Meta:
        abstract = True


class FermentableRecord(ItemRecord):#record of each item
    pass

class HopRecord(ItemRecord):
    pass

class YeastRecord(ItemRecord):
    pass

这在3种不同的Record型号上使用相同的相关名称。 接下来我有:

class Category(models.Model):
    name = models.CharField(max_length=250)
    banned = models.BooleanField(default=False)


class ItemRecord(models.Model):
    catalog_id = models.IntegerField()
    name = models.CharField(max_length=250)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    active = models.BooleanField(default=True) # is the item being sold at all? (carried)
    in_stock = models.BooleanField(default=True) # is the item currently in stock?
    banned = models.BooleanField(default=False)

    class Meta:
        abstract = True


class FermentableRecord(ItemRecord):#record of each item
    category = models.ForeignKey(Category, related_name="fermentable_items", null=True, blank=True)

class HopRecord(ItemRecord):
    category = models.ForeignKey(Category, related_name="hops_items", null=True, blank=True)

class YeastRecord(ItemRecord):
    category = models.ForeignKey(Category, related_name="yeast_items", null=True, blank=True)

现在我有:

class Category(models.Model):
    name = models.CharField(max_length=250)
    banned = models.BooleanField(default=False)


class ItemRecord(models.Model):
    catalog_id = models.IntegerField()
    name = models.CharField(max_length=250)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    active = models.BooleanField(default=True) # is the item being sold at all? (carried)
    in_stock = models.BooleanField(default=True) # is the item currently in stock?
    banned = models.BooleanField(default=False)
    category = models.CharField(max_length=250, null=True, blank=True)

    class Meta:
        abstract = True


class FermentableRecord(ItemRecord):#record of each item
    pass

class HopRecord(ItemRecord):
    pass

class YeastRecord(ItemRecord):
    pass

我可以makemigrations,但我根本无法连接到db。我想重置数据库,因为它正处于开发阶段,但我什么也做不了。 category字段是一个字符串,然后我将它设置为FK,它在清除现有字符串值之前需要一个int(由于错误,所有'Category对象'都在这里)。

2 个答案:

答案 0 :(得分:1)

如果您只想删除类别中的数据,则可以在shell中执行以下操作:

$ python manage.py dbshell # this will start the mysql client
mysql> show tables likes '%_itemrecord'; -- find the table name
mysql> update XXX_itemrecord set category=null; -- this will set all category values to NULL so that you can migrate to FK 

如果您不介意丢失数据,并且您的应用程序名称是app_name,则可以在shell中执行以下操作:

$ python manage.py migrate app_name zero # this will blow away all your current data
$ python manage.py migrate app_name 

答案 1 :(得分:0)

您是否检查类别表中是否存在类别对象,其中ID最初存储在类别字段中。

例如:对于id = 1366

,存在类别对象
Category.objects.filter(id=1366).exists()