Django 1.11是否可以在两个不同数据库的表之间创建关系? [英] Django 1.11 can we create relationship between tables from two different databases?

查看:52
本文介绍了Django 1.11是否可以在两个不同数据库的表之间创建关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我试图创建具有多个数据库的中央数据库服务,所以在这里我的问题是我们可以在两个不同数据库的表之间创建关系吗?

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?

示例:

MySQL DB1.table用户

MySQL DB1.table user

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

MySQLDb2.table帖子

MySQLDb2.table post

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

推荐答案

Django不支持跨数据库的关系,仅正式支持数据库中的单个架构.虽然可能有多种方法可以破解类似的东西,但具体取决于您的数据库风格,不建议这样做.

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.

我在hack上做了一些工作,以便在Django中具有多个架构,而且效果不佳.

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

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

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

祝你好运!

这篇关于Django 1.11是否可以在两个不同数据库的表之间创建关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆