我目前正在开发一个项目,在该项目中我处理公共(public)信息和私有(private)信息,这些信息都以不同的模型存储在公共(public)数据库中。
我想将此数据库分成两部分,一个包含私有(private)模型对象,另一个包含公共(public)模型对象。
问题是,这两个模型彼此之间都有外键关系,并且我在互联网上发现了相互矛盾的答案,即即使模型位于两个不同的数据库中,这种关系是否也有效。
那么,这可能吗?有更好的方法吗?
只是为了澄清我为什么要这样做,我希望该项目是开源的,因此公共(public)数据库应该是公开的,但敏感信息(用户和密码)应该保密。
最佳答案
来自 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.
有关可能的解决方案,请查看此讨论:https://stackoverflow.com/a/32078727/14209813
关于python - Django 中不同数据库中存储的模型之间可以建立关系吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69861463/