3

I have found the same question but not real answer (just let's see with DATABASE_ROUTERS .... ) : see here

So I explain : - I have 2 models A and B (class A .... Class B in model.py) - I have 2 database (db1 which is a SQL database and db2, a NoSQL database) - I have JUST 1 app - Model A must be only on db1 and model B must be only on db2

I would like to do "makemigrations -- db=db1" then "migrate -- db=db1" and to see that the migration is prepared only for the database db1 and that only the model A is created, same thing for db2 and B

is it possible with django ??

Community
  • 1
  • 1
Philippos
  • 103
  • 1
  • 8
  • [Django docs](https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#an-example) provide an example for routers on a per-model basis. Doesn't it do the trick? – blacklwhite Feb 20 '16 at 12:40
  • @blacklwhite To me it appears that example routes on a per-APP basis, not per-model, since it's using a meta app_label. – Ezekiel Kruglick May 05 '16 at 04:27
  • @EzekielKruglick The examples are on a per-app basis, however, you should be able to add model._meta.model_name to do it on a per-model level. – blacklwhite May 10 '16 at 13:46

2 Answers2

2

Django can not split migrations between databases for an application, see https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#allow_migrate.

You have several options:

  1. Let django migrate all models to both databases, and then one table will be left empty on each database. You will need a db router on a row basis, and give the router a hint what database the object belongs to. The read/write routing, unlike migrations, does give the option to decide on the database based on the model (and not only the app).

  2. Same as 1, but to be safe, run a custom DROP TABLE statement for table A on database2, and for table B on database 1. If you will make some routing mistakes, you will get an exception

  3. Split your app. Although django is geared to couple data and functionality together in the same app, you can split an app when needed. In your case, into three:

    • app1 with model A
    • app2 with model B
    • app3 with the views,url, tests etc that will access the database layer in app1, app2.

See Django models across multiple projects/microservices. How to?

Community
  • 1
  • 1
Aviah Laor
  • 3,620
  • 2
  • 22
  • 27
  • Just a question : how do you do that " read/write routing, unlike migrations, does give the option to decide on the database based on the model" ? if model = 'A' : db = 'db1' ? – Philippos Feb 21 '16 at 05:36
  • You have two options: (1) Let django decide, and you define a db router that tells django how to decide. See the complete example https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#an-example (2) If the caller knows where to send the record, then the objects manager has a using() argument to set the db, see https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#manually-selecting-a-database-for-a-queryset – Aviah Laor Feb 21 '16 at 06:04
  • ok I took the code of your first link replacing `if model._meta.app_label == 'A':` by `if model._meta.object_name == 'A':` and it works fine :) Thanks a lot :) – Philippos Feb 21 '16 at 06:51
1

Looks like it is possible. You should setup all databases in the settings DATABASES. And after that you can write custom database router like it is described here: https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#using-routers

But I believe that you have ready read that. More interesting question is what about mirations? And looks like there an option to play with - it is allow_migrate from here. https://docs.djangoproject.com/en/1.9/topics/db/multi-db/#an-example I would try to place here custom logic to handle database miration into the right DB.

Paul
  • 6,641
  • 8
  • 41
  • 56