【问题标题】:MySQL: django.db.utils.OperationalError: (1366, "Incorrect integer value: 'Category object' for column 'category_id' at row 1")MySQL:django.db.utils.OperationalError:(1366,“不正确的整数值:第 1 行的列 'category_id' 的'Category object'”)
【发布时间】:2017-01-15 07:07:55
【问题描述】:

将 MySQL 与 Django 结合使用,我已将模型从使用字符串作为“类别”更改为使用 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,但我什至无法连接到数据库。我只想重置数据库,因为它正在开发中,但我无能为力。 category 字段是一个字符串,然后我将其设置为 FK,它在清除现有字符串值之前需要一个 int(其中所有“类别对象”由于错误而出现)。

【问题讨论】:

  • "但我什至无法连接到数据库。"请解释一下。连不上数据库怎么迁移?
  • 它不会迁移,我的意思是它只会迁移

标签: python mysql django python-3.x


【解决方案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 

【讨论】:

    【解决方案2】:

    您是否检查过 类别对象 存在于 类别 表中,并且 id 最初存储在类别字段中。

    例如:存在 id = 1366 的类别对象

    Category.objects.filter(id=1366).exists()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-24
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多