Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Django 1.11 from creating migrations for unmanaged models?

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?

like image 246
Cerin Avatar asked May 03 '18 16:05

Cerin


1 Answers

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.

like image 54
Brown Bear Avatar answered Sep 25 '22 12:09

Brown Bear