9

I have a Django 1.11.10 project with an unmanaged model like:

class MyModel(models.Model):

    id = models.PositiveIntegerField(primary_key=True)

    name = models.CharField(max_length=500)

    class Meta:
        managed = False

The model wraps a custom SQL view. However, when I run manage.py makemigrations, I find that Django tries to generate a migration that creates a traditional SQL table for this model.

In past versions of Django, as this question illustrates, managed = False used to prevent this. Is this no longer true? How do you get Django to not ignore schema changes on a model?

Cerin
  • 60,957
  • 96
  • 316
  • 522
  • 1
    Are you sure the migration attempts to create table? Just tried, migration also contains 'managed=False', and `sqlmigrate` does not produce SQL for creating table – awesoon May 03 '18 at 16:32
  • @soon You seem to be correct. However, why does Django generate a schema migration that makes no schema changes? – Cerin May 03 '18 at 16:49
  • @Cerin are you sure? There are actually 2 types of migrations database changes and state changes https://docs.djangoproject.com/en/1.11/ref/migration-operations/#separatedatabaseandstate – Sardorbek Imomaliev May 03 '18 at 17:23
  • To me it looks like an API bug and I have the same issue BTW. – yǝsʞǝla Apr 21 '20 at 09:42

1 Answers1

4

Inside the migrations file you can see options More infos can be found here)

    options={
        'managed': False,
    },

that mean do not create a table, and as @soon say you can look on sqlmigrate, and something like this

$ ./manage.py sqlmigrate YOUR_APP_NAME MIGRATION_NUMBER

BEGIN;
--
-- Create model MyModel
--
COMMIT

No real table created, but django need the migration to control changes of the model.

To exclude collisions, while Django do migrations it get model description not from current model files, but restore it step by step from files inside the migrtaions folder.

Lemayzeur
  • 8,297
  • 3
  • 23
  • 50
Brown Bear
  • 19,655
  • 10
  • 58
  • 76