简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM