I'm building a website under Django Framework, this website needs to have different SQL schemes, for now I succeeded creating all schemes and all stuff, but I don't understand why the table django_migrations is in each schema after migration of databases.
Expected Databases Content:
AppDB tables are all the models defined by this app
Default DB tables are all Django tables (admin, contenttypes, auth, sessions)
Databases Content:
AppDB tables are all the models defined by this app + django_migrations
DEFAULT tables are all Django tables (admin, contenttypes, auth, sessions) + django_migrations
Those are the routers of the 2 dbs:
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.
Django Database Migrations. Migration is a way of applying changes that we have made to a model, into the database schema. Django creates a migration file inside the migration folder for each model to create the table schema, and each table is mapped to the model of which migration is created.
The migrations table is not optional, it must exist on each database so it can track which migrations have been applied on that database.
It's the mechanism by which the Django migration system understands the current state of the database and which migrations need to be run. So it's required on all the databases. Now, if you had a table that didn't actually need migrations—say, a read-only database—then this could cause problems. That's the subject of this ticket.
The migrations system will maintain backwards-compatibility according to the same policy as the rest of Django, so migration files generated on Django X.Y should run unchanged on Django X.Y+1. The migrations system does not promise forwards-compatibility, however.
The django_migrations
table records which migrations have been applied on that database. It's the mechanism by which the Django migration system understands the current state of the database and which migrations need to be run. So it's required on all the databases.
Now, if you had a table that didn't actually need migrations—say, a read-only database—then this could cause problems. That's the subject of this ticket.
Prior to Version 1.7 of Django, there was no django_migrations table. Thereafter, Django rightly included migrations for handling database schema related changes, that is changes in field definition, addition or deletion of fields in db.models.
In Earlier versions, developers use django_south_migration app for doing exactly this but since almost everybody was using it, Django included in 1.7 version onwards.
Now coming to your question, django_migrations table keep records of changes in database schema applied to database. This table helps django in applying new migrations created after
python manage.py makemigrations
.
app column of this table records name of the app to which this migration was applied. If you will go to migrations directory of any django app, you will see migration files of the form 0001_auto.py etc.
eg if this migration has been applied to database, you will find an entry with name=0001_auto and app= in django_migrations table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With