Saturday, December 3, 2022

Re: Model with shared table among schemas

If you can figure out how to set up a separate database router for your shared table, you can set allow_migrate = False on one of the instances.

On December 3, 2022 10:57:10 AM CST, Paolo Miliaris <paolo.perliti@gmail.com> wrote:
Hi all.
I have a simple Django app deployed as **two separated instances** on same server. These instances need to be separeted (same DB, different schemas) and they don't need to interect each other except for a single table (`bm_catalog`, coupled to its corresponding `class Catalog` - which estends `models.Model`). The model `Catalog` is used by both app's instances to read common data.
I decided to store this shared table in the **public** schema:

```
class Catalog(models.Model):
    description = models.CharField(max_length=255)
    link = models.CharField(max_length=255, null=True)

    class Meta:
        db_table = '"public"."bm_catalog"'
```

`bm_catalog` is owned by user 'postgres' and granted to be accessed by other schemas.
After some adjustments it finally works but I have 2 questions:

1. When i run `manage.py migrate` on the first instance it works, but when I run same command on the second instance I get an error saying the table (obviously) already exists. Nontheless (sooner or later) I need to migrate both instances...

2. Everytime I will have to alter the class Catalog I will run in the same previous issue.

Do you have any advice? (or an alternative approach to suggest?)
I would avoid to use multi-tenant packages, if possible.

Thanks in advance,
Paolo

No comments:

Post a Comment