0

I've got a Django app called dataapi which is built by introspecting a PostgreSQL database to write various models files (one for each schema). It works and provides us read-only access to the data we need via the ORM, but I break up models into multiple files by schema rather than putting them all in a (huge) models.py file.

I've defined a ROOT/config/routers.py file tell Django I don't want this app's models to use migrations:

class DataAPIRouter(object):
    """
    A router to control all database operations on models in the
    dataapi application.
    """

    def db_for_read(self, model, **hints):
        """
        Attempts to read dataapi models go to mssqlwrds.
        """

        if model._meta.app_label == 'dataapi':
            return 'pgdata'
        return None


    def db_for_write(self, model, **hints):
        """
        Attempts to write dataapi models go to mssqlwrds.
        """

        if model._meta.app_label == 'dataapi':
            return 'pgdata'
        return None


    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the dataapi app is involved.
        """

        if obj1._meta.app_label == 'dataapi' or obj2._meta.app_label == 'dataapi':
            return True
        return None


    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the dataapi app doesn't use migrations.
        """

        if app_label == 'dataapi':
            return False
        return True

In my settings, I've defined the router:

DATABASE_ROUTERS = [
    'config.routers.DataAPIRouter',
]

Yet when I run a dry run using makemigrations, it still shows that it's hitting the app:

(project) [vagrant@vagrant project]$ ./manage.py makemigrations --dry-run
Migrations for 'dataapi':
  0001_initial.py:
    - Create model aco_amda
    - Create model aco_imda
    - Create model aco_indfnta
    - Create model aco_indfntq
    [...]

Am I missing something? How can I get this app to be ignored by migrations?

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • It is ignored. `allow_migrate` prevents the _execution_ of migrations in the `dataapi` app. It still creates the migrations, that's just how it works. – knbk Aug 15 '16 at 14:04
  • Ah, thanks for the clarifications. I'll issue a PR to update the documentation with this clarification. Much appreciated! If you submit this as an answer, I'll mark it correct. – FlipperPA Aug 15 '16 at 14:23

1 Answers1

0

As it turns out, this is by design. As @knbk pointed out, this setting in the router only stops the migration from happening when you execute python manage.py migrate. It will still create the migrations when you run python manage.py makemigrations. I have issued a PR to Django to clarify this in the documentation which has been accepted.

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • Hey. I just ran into this. So am I supposed to delete the generated migration? If I don't delete it, the app shows up in the list of migrations during a `migrate` command, e.g. `Operations to perform: Apply all migrations: app_that_shouldnt_show_up, auth, contenttypes, sessions. Running migrations: Applying app_that_shouldnt_show_up.0001_initial... OK ...` – Shadi Nov 15 '17 at 08:57
  • 1
    It has been a while since I worked on this, but I believe I just faked it with `python manage.py migrate appname --fake`. That'll mark it as run in the database without actually running it, IIRC. – FlipperPA Nov 15 '17 at 14:28