-1

I don't want to use Fake app migrate option for the solution Please suggest any other method for this problem

Do check my code

Models - from django.db import models

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

Serializer -

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__"

View -

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

Database name if hello and table name is 'GAUR' as mention in model.

I tried with same syntax with SQLite configuration, it works for SQLite but when I want to work on database thing it shows the given error on -

python manage.py migrate 

error is -

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(self, query) django.db.utils.OperationalError: (1050, "Table 'gaur' already exists")

python manage.py makemigrations 

Please suggest a good solution because I have seen all the links of stack for the similar errors, but does give me desired output.

Virtual environment name is - myproject

I tried my making different project under different virtual environment in case the environment is corrupted, but it didnt work.

I even tried on different location with same virtual environment but result was same.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gaurav Gupta
  • 405
  • 7
  • 23
  • Is this a new project? If so, you can try to revert back the django migration using the steps in https://stackoverflow.com/a/32124113/690576 and then run the migration again. – JRajan Nov 28 '19 at 07:27

1 Answers1

1

I have encountered this problem, i was scratching my head then i came to know if you simply remove migrations and try again it will work, the problem is when you delete or edit something directly in models.py and try to migrate it will raise this error as already exists, Its not the way to do it,even though you delete or change directly in models.py its not reflected in migrations part, so it will raise error as already exists. Here is the part taken from.

  1. Remove the all migrations files within your project Go through each of your projects apps migration folder and remove everything inside, except the init.py file.

Or if you are using a unix-like OS you can run the following script (inside your project dir):

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

that's it.