【问题标题】:operational error 1050 Error “Table already exists” in django makemigrationsdjango makemigrations 中的操作错误 1050 错误“表已存在”
【发布时间】:2019-09-10 21:32:09
【问题描述】:

您好,无论我尝试对模型进行更改或尝试迁移,都会遇到此问题 我正在使用 mysql 和 django 1.8.6

操作错误 1050 错误“表 'products_myproducts' 已存在” 每次我进行迁移时

class Product(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    managers = models.ManyToManyField(settings.AUTH_USER_MODEL,related_name="managers_product",blank=True)
    media = models.ImageField(blank=True,null=True,
            upload_to=download_media_location,
            storage=FileSystemStorage(location=settings.PROTECTED_ROOT))
    title = models.CharField(max_length=30)
    description = models.TextField(default='',blank=True)
    slug= models.SlugField(blank=True,unique=True)
    price = models.DecimalField(max_digits=60,decimal_places=2,default=9.99)
    sale_active = models.BooleanField(default=False)
    sale_price = models.DecimalField(max_digits=60,decimal_places=2,default=6.99,null=True,blank=True)
    def __str__(self):
        return self.title

也适用于我的产品

class MyProducts(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)
    products = models.ManyToManyField(Product,blank=True)

    def __str__(self):
        return "%s" %(self.products.count())

    class Meta:
        verbose_name = "My Products"
        verbose_name_plural = "My Products"

【问题讨论】:

    标签: python mysql django database


    【解决方案1】:

    当您从应用中删除迁移目录而不删除数据库表时,会导致此错误。
    如果数据库上的数据很重要,只需备份表单数据,然后删除数据库表。
    然后从每个应用程序中删除所有迁移目录并 makemigration 然后再次迁移并导入您的数据。

    【讨论】:

    • 我应该也删除默认的 dajango 表
    • @Cipher Yes.drop all tables
    • 当我进行迁移时说没有检测到更改
    • @Cipher 你删除了每个应用中的所有迁移文件夹吗?
    • 是的,我的所有应用程序中都没有迁移文件夹
    猜你喜欢
    • 2011-03-19
    • 1970-01-01
    • 2016-01-18
    • 2018-02-18
    • 2016-03-13
    • 1970-01-01
    • 2011-10-24
    • 2013-12-13
    • 2010-12-11
    相关资源
    最近更新 更多