Django中的动态数据库路由 [英] Dynamic database routing in Django

查看:294
本文介绍了Django中的动态数据库路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库中,我在数据库中定义了一个 Customer 表,所有其他表都是外键输入的。

In my database, I have a Customer table defined in my database that all other tables are foreign keyed on.

class Customer(models.Model):
    ...

class TableA(models.Model):
    Customer = models.ForeignKey(Customer)
    ...

class TableB(models.Model):
    Customer = models.ForeignKey(Customer)
    ...

我正在尝试实现一个数据库路由器,该路由器根据 Customer 表。例如,范围为1-100的 id s将连接到数据库A,范围为101-的 id s- 200将连接到数据库B。

I'm trying to implement a database router that determines the database to connect to based on the primary key of the Customer table. For instance, ids in the range 1 - 100 will connect to Database A, ids in the range 101 - 200 will connect to Database B.

我已阅读路由器,但我不确定我要问的内容是否可行。具体来说,方法 db_for_read(model,**提示) db_for_write(model,**提示) 类型。这对我没有用,因为我需要基于对象实例的内容进行路由。该文档进一步指出,当前提供的唯一 **提示 instance 对象(在适用情况下)完全没有提供实例。这并没有激发我信心,因为它没有明确说明没有提供实例的情况。

I've read through the Django documentation on routers but I'm unsure if what I'm asking is possible. Specifically, the methods db_for_read(model, **hints) and db_for_write(model, **hints) work on the type of the object. This is useless for me as I need routing to be based on the contents of the instance of the object. The documentation further states that the only **hints provided at this moment are an instance object where applicable and in some cases no instance is provided at all. This doesn't inspire me with confidence as it does not explicitly state the cases when no instance is provided.

我实质上是在尝试实现数据库的应用程序级分片。

I'm essentially attempting to implement application level sharding of the database. Is this possible in Django?

推荐答案

解决鸡肉和鸡蛋问题


解决保存新客户时的鸡肉和鸡蛋问题。您必须保存才能获得ID,但必须知道ID才能知道将保存在何处。

Solve Chicken and egg

You'll have to solve the chicken and egg problem when saving a new Customer. You have to save to get an id, but you have to know the id to know where to save.

您可以通过以下方法解决此问题:首先将所有客户保存在DatabaseA中,然后检查ID和也将其保存在目标数据库中。请参见 Django multidb:写入多个数据库。因此,如果您这样做,则不会遇到这些问题。但是请确保注意删除客户。

You can solve that by saving all Customers in DatabaseA first and then check the id and save it in the target db too. See Django multidb: write to multiple databases. If you do it consequently, you won't run into these problems. But make sure to pay attention to deleting Customers.

如果提示中有 instance 是客户,然后返回 DatabaseA,或者是客户,然后确定其customer_id或customer.id。

The routing problem that's left is pretty straight forward if an instance is in the hints. Either it is a Customer and you'll return 'DatabaseA' or it has a customer and you'll decide on its customer_id or customer.id.

如果提示中没有实例,但它是您应用程序中的模型,则会引发错误,因此您可以更改创建查询集的代码。当没有自动添加提示时,您应该始终提供提示。

When there is no instance in the hints, but it is a model from your app, raise an error, so you can change the code that created the Queryset. You should always provide hints, when they aren't added automatically.

如果对于大多数查询您都知道顾客,没关系。但是考虑一下 TableA.objects.filter(customer__name__startswith ='foo')

If for most queries you have a know Customer, this is ok. But think about queries like TableA.objects.filter(customer__name__startswith='foo')

这篇关于Django中的动态数据库路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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