Django 1.11可以在来自两个不同数据库的表之间创建关系吗?

时间:2022-02-11 15:25:05

In my project I'm trying to create central DB service with multiple database so here my question is can we create relationship between tables from two different databases?

在我的项目中,我尝试用多个数据库创建*数据库服务,因此我的问题是,我们能否在两个不同的数据库之间创建表之间的关系?

Example:

例子:

MySQL DB1.table user

MySQL DB1。用户表

class User(models.Model):
     name = models.CharField()

MySQLDb2.table post

MySQLDb2。表文章

class Post(models.Model):
     title = model.CharField()
     user= models.Forignkey(User)

1 个解决方案

#1


1  

Django doesn't support relationships across databases, and only officially supports a single schema within a database. While there may be ways to hack something like this up, depending on your flavor of database, it is not recommended.

Django不支持跨数据库的关系,只支持数据库中的单个模式。虽然可能会有一些方法来解决类似的问题,但这取决于您的数据库风格,因此不推荐使用它。

I've done some work with hacks to have multiple schemata in Django, and it isn't pretty.

我在Django中使用了一些hack来实现多个模式,这并不好。

You may want to read this part of the documentation:

您可能想要阅读文档的这一部分:

https://docs.djangoproject.com/en/2.0/topics/db/multi-db/#limitations-of-multiple-databases

https://docs.djangoproject.com/en/2.0/topics/db/multi-db/ limitations-of-multiple-databases

Good luck!

好运!

#1


1  

Django doesn't support relationships across databases, and only officially supports a single schema within a database. While there may be ways to hack something like this up, depending on your flavor of database, it is not recommended.

Django不支持跨数据库的关系,只支持数据库中的单个模式。虽然可能会有一些方法来解决类似的问题,但这取决于您的数据库风格,因此不推荐使用它。

I've done some work with hacks to have multiple schemata in Django, and it isn't pretty.

我在Django中使用了一些hack来实现多个模式,这并不好。

You may want to read this part of the documentation:

您可能想要阅读文档的这一部分:

https://docs.djangoproject.com/en/2.0/topics/db/multi-db/#limitations-of-multiple-databases

https://docs.djangoproject.com/en/2.0/topics/db/multi-db/ limitations-of-multiple-databases

Good luck!

好运!