W Kenny
W Kenny

Reputation: 2069

Django change Migration table name in DB

How can I custom the django_migrations table name?

Even I edit the

venv.lib.site-packages.django.db.migrations.recorder > MigrationRecorder > meta = db_table = "ABC_django_migrations"

the makemigrations cannot detect the changes.

I am using Django version: 3.0.5

Upvotes: 3

Views: 1479

Answers (2)

W Kenny
W Kenny

Reputation: 2069

How I solve this problem:

  1. Install the app

    pip install django-db-prefix
    
  2. Include the apps in Settings.py

    INSTALLED_APPS = ['django_db_prefix',]
    
  3. Adding the prefix in Settings.py

    DB_PREFIX = "foo_"
    

Upvotes: 1

iklinac
iklinac

Reputation: 15728

Django migrations don't look for changes in its own migration model

MigrationExecutor just ensures that following table exists in database

def migrate(self, targets, plan=None, state=None, fake=False, fake_initial=False):

    self.recorder.ensure_schema()

    ....

where ensure_schema() just creates the table

def ensure_schema(self):
    """Ensure the table exists and has the correct schema."""
    # If the table's there, that's fine - we've never changed its schema
    # in the codebase.
    if self.has_table():
        return
    # Make the table
    try:
        with self.connection.schema_editor() as editor:
            editor.create_model(self.Migration)
    except DatabaseError as exc:
        raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)

you can manually make migration to edit this model ( AlterModelTable or custom sql) but I would not advise changing anything regarding migrations

Upvotes: 3

Related Questions