1

Firstly please let me introduce my use-case: I am working on Django application (GraphQL API using Graphene), which runs in the cloud but also have its local instances in local customer's networks.

For example One application in the cloud and 3 instances (local Django app instance with a PostgreSQL server with enabled BDR) on local networks. If there is a network connection we are using bi-directional replication to have fresh data because if there is no connectivity we use local instances. Here is the simplified infrastructure diagram for an illustration.

Architecture example

So, if I want to use the BDR I can't do DELETE and UPDATE operations in ORM. I have to generate UUIDs for my entities and every change is just a new record with updated data for the same UUID. Latest record for selected UUID is my valid record. Removal is just a another flag. Till now, everything seems to be fine, problem starts when I want to use for example many-to-many relationship. Relationship relies on the database primary keys and I have to handle removal somehow. Can you please find the best way how to solve this issue? I have few ideas but I do not want to made a bad decision:

  1. I can try to override ManyToManyField to work with my UUIDs and special removal flag. It's looks like nice idea because everything should work as before (Graphene will find the relations etc.). But I am afraid of "invisible" consequences.
  2. Create my own models to simulate ManyToMany relationship. It's much more work but it should work just fine.

Did you have to solve similar issue before? Is there some kind of good practice or it's just building a highway to hell (AC/DC is pretty cool)?

Or if you think there is a better way how to build the service architecture, I would love to hear your ideas.

Thanks in advance.

Jakub Dubec
  • 77
  • 1
  • 11
  • 1
    Under the hood, a ManyToMany relationship creates the relations table (object with two foreign keys). Since you need to add information about the relationship (like the removal flag) you might want to use `through` (see [this](https://docs.djangoproject.com/en/dev/topics/db/models/#intermediary-manytomany)) on your many2many relationship. The `through` model can be managed by you like your other models (same procedure for DELETE and UPDATE). But you could still use some of Django's shortcut methods to fetch all related objects, etc... – dirkgroten Dec 06 '18 at 15:35
  • @dirkgroten thank you for reply, `through`attribute is definitely resolving a half of the issue. But I still have to find the way, how to override relationship resolving in the Django ORM, because there will be record for every change of the relation state in the past and I looking for the latest. In the end, I will have a lot of states in same table for every record and I need to resolve only the latest one. (Every change is a new row in the table with same record UUID). – Jakub Dubec Dec 13 '18 at 16:19

0 Answers0