1

I'm currently working on a project where I handle both public and private information, both stored as different models in a common database.

I would like to split this database in two, one with the private model objects and another one with the public ones.

The thing is, both this models have a ForeignKey relationship with each other, and I've found conflicting answers over the internet about if this relationships can work even if the models are in two different databases.

So, is this possible? Is there a better approach for doing this?

Just to clarify why I want to do this, I want the project to be open source, therefore the public database should be public, but the sensitive information (users and passwords) should be kept private.

Marcos
  • 71
  • 1
  • 4

1 Answers1

1

From Django docs:

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.

This is because of referential integrity. In order to maintain a relationship between two objects, Django needs to know that the primary key of the related object is valid. If the primary key is stored on a separate database, it’s not possible to easily evaluate the validity of a primary key.

For possible solutions check out this discussion: https://stackoverflow.com/a/32078727/14209813

yagus
  • 1,417
  • 6
  • 14