Python MySQL:django.db.utils.OperationalError:(1366,“不正确的整数值:';类别对象';用于第1行的列';类别id'”)
在Django中使用MySQL,我将模型从使用字符串作为“category”更改为使用FK。它现在已被损坏Python MySQL:django.db.utils.OperationalError:(1366,“不正确的整数值:';类别对象';用于第1行的列';类别id'”),python,mysql,django,python-3.x,Python,Mysql,Django,Python 3.x,在Django中使用MySQL,我将模型从使用字符串作为“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.IntegerF
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种不同的记录模型上使用相同的相关名称不同。
接下来我有:
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,在清除现有字符串值(由于错误,所有“category object”都在该字符串中)之前,它需要一个int。是否检查了category object表中是否存在category,id最初存储在category字段中
例如:id=1366的类别对象存在
Category.objects.filter(id=1366).exists()
如果您只想删除类别中的数据,则可以在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
$ python manage.py migrate app_name zero # this will blow away all your current data
$ python manage.py migrate app_name
如果您不介意丢失数据,并且应用程序名为app_name,则可以在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
$ python manage.py migrate app_name zero # this will blow away all your current data
$ python manage.py migrate app_name
“但我甚至连数据库都无法连接。”请解释一下。如果无法连接到数据库,如何进行迁移?它不会迁移,我的意思是它只进行迁移