【问题标题】:Django _mysql.connection.query(self, query) django.db.utils.OperationalError: (1050, "Table 'gaur' already exists")Django _mysql.connection.query(self, query) django.db.utils.OperationalError: (1050, "Table 'gaur' already exists")
【发布时间】:2019-11-28 07:02:58
【问题描述】:

我不想为解决方案使用假应用迁移选项 请为这个问题提出任何其他方法

请检查我的代码

型号 - 从 django.db 导入模型

from mptt.models import MPTTModel, TreeForeignKey

class Delhi(models.Model):
    account_id = models.IntegerField()
    type_code = models.CharField(max_length=200)
    sub_type_code = models.CharField(max_length=200)
    name = models.CharField(max_length=200)
    credit_amount = models.IntegerField()
    debit_amount = models.IntegerField()
    # parent = TreeForeignKey('self', null = True, related_name = 'strctr', on_delete = models.CASCADE)


    class Meta:
        managed = True
        db_table = 'gaur'

    def __str__(self):
        return self.type_code


class Ranchi(MPTTModel):
    node_name = models.CharField(max_length = 100)
    parent = TreeForeignKey('self', null = True, related_name = 'strctr', on_delete = models.CASCADE)


    def __str__(self):
        return self.name

序列化器 -

from rest_framework import serializers
from .models import Delhi, Ranchi

class DelhiSerializer(serializers.ModelSerializer):
    class Meta:
        model = Delhi
        fields = "__all__"

class RanchiSerializer(serializers.ModelSerializer):
    class Meta:
        model = Ranchi
        fields = "__all__"

查看 -

from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import generics 
from rest_framework import status 



class CreateView(generics.ListCreateAPIView):                        
    """This class defines the create behavior of our rest api."""
    queryset = Delhi.objects.all()
    serializer_class = DelhiSerializer

    def perform_create(self, serializer):
        """Save the post data when creating a new bucketlist."""
        serializer.save()



class DetailsView(generics.RetrieveUpdateDestroyAPIView):             
    """This class handles the http GET, PUT and DELETE requests."""

    queryset = Delhi.objects.all()
    serializer_class = DelhiSerializer

如果 hello 和表名是模型中提到的“GAUR”,则为数据库名称。

我尝试使用与 SQLite 配置相同的语法,它适用于 SQLite,但是当我想处理数据库时,它会在 -

上显示给定的错误
python manage.py migrate 

错误是 -

Operations to perform:
  Apply all migrations: admin, app1, auth, contenttypes, sessions
Running migrations:
  Applying app1.0001_initial...Traceback (most recent call last):

。 . . . . . _mysql.connection.query(自我,查询) django.db.utils.OperationalError: (1050, "表 'gaur' 已经存在")

python manage.py makemigrations 

请提出一个好的解决方案,因为我已经看到堆栈的所有链接都存在类似错误,但确实给了我想要的输出。

虚拟环境名称是-myproject

我尝试在不同的虚拟环境下制作不同的项目,以防环境损坏,但没有奏效。

我什至在不同的位置尝试了相同的虚拟环境,但结果是一样的。

【问题讨论】:

标签: python django python-3.x django-rest-framework django-views


【解决方案1】:

我遇到了这个问题,我一直在摸不着头脑然后我才知道如果你只是删除迁移并重试它会起作用,问题是当你直接在 models.py 中删除或编辑某些内容并尝试迁移它时将引发这个已经存在的错误,它不是这样做的方法,即使你直接在models.py中删除或更改它也不会反映在迁移部分中,所以它会引发已经存在的错误。 Here is the part taken from.

  1. 删除项目中的所有迁移文件 浏览您的每个项目应用程序迁移文件夹并删除其中的所有内容,init.py 文件除外。

或者,如果您使用的是类 unix 操作系统,您可以运行以下脚本(在项目目录中):

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -deleteenter code here

就是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2021-07-29
    • 2020-04-26
    相关资源
    最近更新 更多