'Django makemigrations show no changes detected after table rename in mysql

I have a Django application with a My-SQL database. recently I alter the table_name with the help of MySQL query in the MySQL-shell, after this when I run makemigration and migrate command terminal says "No changes detected". how can i resolve this issue and create again this table with help of Django makemigration and migrate?

can I delete a table from MySQL, any possibility will Django create it again?



Solution 1:[1]

If you renamed your table outside Django - you will have to tell Django the new table name like so (using the Meta class):

class Model(models.Model):
    name = models.CharField(max_length=255)

    class Meta:
        db_table = 'new_table_name'

To re-create your table using existing model you need to reset migration for that app to zero and then run migration again

python manage.py migrate APP_NAME zero
python manage.py migrate APP_NAME

Solution 2:[2]

It's because the migrations table managed by django doesn't reflect the correct db schema since it was already modified outside of django. If you don't have any important data you can do a migration rollback or recreate the table by hand.

The best way to dela with this is to rename your table back to the original name. Then create a blank migration inside your app and recreate the sql commands you did in the shell inside that migration file. That way django can keep track of the database schema.

Solution 3:[3]

You should change the name of the table in models.py not in MySQL shell.

From

class MyModel(models.Model):
   ...

To

class ThisModel(models.Model):
   ...

Or Create Proxy Model :

class ThisModel(MyModel):
    class Meta: 
       proxy = True 
       verbose_name = "ThisModel"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3