[Django]-Multiple databases in django – syncdb doesn't create django_ tables

3👍

Just a comment:
My idea was to use a few databases, which will communicate within each other (eg. using Foreign keys from one DB to another one).
I wish i read the documentation more carefully:

Cross-database relations
Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.

All that mess was for nothing, because integrity constraints dont allow to use any relationships over multiple databases.

👤Boun

1👍

What helped me is this post:
http://diegobz.net/2011/02/10/django-database-router-using-settings/

Solution is this:

  1. using just one Router, and this router will take care of application 2 – router app2.router.App2Router,not any routers for default database. Routers for default database works fine just as it is.

  2. Alter that app2.router.App2Router to look like the code in that link in the beginning of the post provided. Doing just step 1 led to creating auth_ and django_ tables (by running syncdb) correctly, but unfortunately ALL MODEL tables were created to default database (ie. also tables from app2). Altering app2.router.App2Router made everything work.

  3. Alter settings.py to look like this:

    DATABASE_ROUTERS = [‘app2.router.DatabaseAppsRouter’]

    DATABASE_APPS_MAPPING = {‘app2’: ‘app2’}

Now running syncdb database=app2 creates tables to database app2 and running syncdb creates all django system tables and own app1 tables to default. Awesome!

Thanks Jason for his ideas!

👤Boun

Leave a comment