Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django: exclude models from migrations

In my django application (django 1.8) I'm using two databases, one 'default' which is MySQL, and another one which is a schemaless, read-only database. I've two models which are accessing this database, and I'd like to exclude these two models permanently from data and schema migrations:

  • makemigrations should never detect any changes, and create migrations for them
  • migrate should never complain about missing migrations for that app

So far, I've tried different things, all without any success:

  • used the managed=False Meta option on both Models
  • added a allow_migrate method to my router which returns False for both models

Does anyone have an example of how this scenario can be achieved? Thanks for your help!

like image 923
thekorn Avatar asked Oct 28 '15 07:10

thekorn


People also ask

What is the difference between Makemigrations and migrate in Django?

migrate , which is responsible for applying and unapplying migrations. makemigrations , which is responsible for creating new migrations based on the changes you have made to your models.

How do I stop migrations in Django?

You can selectively disable migration for one or more Django-Models by setting managed = False in Django Model Meta options. Save this answer.

How do I rollback migrations in Django?

Using django 2.1, this works for me: /manage.py migrate my_app 0010 , only 0010 , not the full file name. After that, manually delete the local files appname/migrations/0011+, then manually delete row(s) from django_migrations db table for 0011+.


2 Answers

So far, I've tried different things, all without any success:

  • used the managed=False Meta option on both Models

That option (the managed = False attribute on the model's meta options) seems to meet the requirements.

If not, you'll need to expand the question to say exactly what is special about your model that managed = False doesn't do the job.

like image 101
bignose Avatar answered Oct 18 '22 15:10

bignose


I thought, I have a problem with makemigrations. It pretends to make migration on managed = False model, but no SQL code generated for this model

Here is my example, model Smdocumets unmanaged, and no SQL code was generated.

python manage.py makemigrations

Migrations for 'monitor':
  monitor\migrations\0005_auto_20171102_1125.py
    - Create model Smdocuments
    - Add field sid to db
    - Alter field name on db

python manage.py sqlmigrate monitor 0005

BEGIN;
--
-- Create model Smdocuments
--
--
-- Add field sid to db
--
ALTER TABLE "monitor_db" RENAME TO "monitor_db__old"; 
...
like image 42
Lamak Avatar answered Oct 18 '22 15:10

Lamak