Ryan Lecha
Ryan Lecha

Reputation: 111

Use Django Migrations to delete a table

I am moving from Rails to Django and have an existing database. I created models.py via python manage.py inspectdb > models.py, made changes, and tested some endpoints. Everything seems fine.

I then ran python manage.py makemigrations and migrate to make the initial django mirgation.

I noticed an old Rail's specific Model / table called ArInternalMetadata / ar_internal_metadata. I figure I could easily remove the table by simply removing the model from models.py and rerun makemigrations however when I do that, django just says No changes detected. Running migrate doesn't remove the table either.

Upvotes: 6

Views: 11608

Answers (1)

Ryan Lecha
Ryan Lecha

Reputation: 111

Figured it out. When inspectdb creates Models from an existing database, it sets managed in the Meta inner class to False by default.

class AssetCategories(FacadeModel):
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()
    name = models.CharField(max_length=255)
    deleted_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'asset_categories'

Per the Django 2.1 Docs this will prevent deletion from happening

If False, no database table creation or deletion operations will be performed for this model

Removing managed = False from the Meta class allows makemigrations / migrate to delete the table.

Upvotes: 4

Related Questions