2017-01-15 89 views
0

使用MySQL使用Django,我已經從使用字符串爲「變化的模型類別「使用FK。現在是打破與MySQL的:django.db.utils.OperationalError:(1366,「不正確的整數值:‘類別對象’的第1行的列‘CATEGORY_ID’」)

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其預計清除現有的字符串值前一個int(這其中所有的「類別對象」由於錯誤)。

+0

「但是我可以根本不連接到數據庫。「請解釋一下。如果無法連接到數據庫,如何遷移? – e4c5

+0

它不會遷移,我的意思是它只是遷移 – codyc4321

回答

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,則可以在您的外殼中執行以下操作:

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

你有沒有檢查類別對象類別表ID最初存儲在類別字段存在。

例如:分類對象存在的id = 1366

Category.objects.filter(id=1366).exists() 
相關問題