Why django_migrations table in all databases

I am creating a site under the Django Framework, this website should have different SQL schemas, at the moment I managed to create all the schemas and everything else, but I do not understand why the django_migrations table is in each schema after the database migration.

  • Expected database content:

    AppDB Tables are all the models defined by this application.

    The default DB tables are all Django tables (admin, contenttypes, auth, sessions)

  • Database Content:

    AppDB tables are all the models defined by this application + django_migrations

    DEFAULT tables are all Django tables (admin, contenttypes, auth, sessions) + django_migrations

These are 2 dbs routers:

class DefaultRouter(object): APPS = ['auth', 'sessions', 'admin', 'contenttypes'] DB = 'default' def db_for_read(self, model, **hints): if model._meta.app_label in self.APPS: return self.DB return None def db_for_write(self, model, **hints): if model._meta.app_label in self.APPS: return self.DB return None def allow_relation(self, obj1, obj2, **hints): if obj1._meta.app_label in self.APPS or obj2._meta.app_label in self.APPS: return True return None def allow_migrate(self, db, app_label, model_name=None, **hints): if app_label in self.APPS: return db == self.DB return None class MyAppDBRouter(object): def db_for_read(self, model, **hints): return self.check_app_label(model) def db_for_write(self, model, **hints): return self.check_app_label(model) def allow_relation(self, obj1, obj2, **hints): if obj1._meta.app_label == 'myapp' or obj2._meta.app_label == 'myapp': return True return None def allow_migrate(self, db, app_label, model_name=None, **hints): if app_label == 'myapp': return db == 'appdb' return None @staticmethod def check_app_label(model): if model._meta.app_label == 'myapp': return 'appdb' return None 

Thanks.

+5
source share
2 answers

The django_migrations table records which migrations were applied to this database. This is the mechanism by which the Django migration system understands the current state of the database and what migrations need to be performed. This is required for all databases.

Now, if you have a table that doesnโ€™t actually need migrations, such as a read-only database, this can cause problems. This is the theme of this ticket .

0
source

Prior to version 1.7 of Django, there was no django_migrations table. After that, Django rightly included migrations to handle changes to the database schema, that is, changes to the definition of fields, adding or removing fields in db.models.

In earlier versions, developers use the django_south_migration application to do just that, but since almost everyone has used it, Django is included in version 1.7 onwards.

Now, turning to your question, the django_migrations table saves the database schema change records that apply to the database. This table helps django apply the new migrations created after python manage.py makemigrations .

In the application column of this table, the name of the application to which this migration was applied is recorded. If you go to the migration directory of any django application, you will see migration files of the form 0001_auto .py, etc.
for example, if this migration was applied to the database, you will find an entry with the name = 0001_auto and app = in the django_migrations table.

0
source

All Articles