0

I need to make sure that an object (Device) is only saved once and only to one database.

I have several PostGre SQL databases as so:

                                       List of databases
             Name              | Owner | Encoding |  Collate   |   Ctype    | Access privileges 
-------------------------------+-------+----------+------------+------------+-------------------
 admin                         | admin | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres                      | admin | UTF8     | en_US.utf8 | en_US.utf8 | 
 reference                     | admin | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0                     | admin | UTF8     | en_US.utf8 | en_US.utf8 | =c/admin         +
                               |       |          |            |            | admin=CTc/admin
 template1                     | admin | UTF8     | en_US.utf8 | en_US.utf8 | =c/admin         +
                               |       |          |            |            | admin=CTc/admin
 workspace_A                   | admin | UTF8     | en_US.utf8 | en_US.utf8 | 
 workspace_B                   | admin | UTF8     | en_US.utf8 | en_US.utf8 | 
 workspace_C                   | admin | UTF8     | en_US.utf8 | en_US.utf8 | 
 workspace_D                   | admin | UTF8     | en_US.utf8 | en_US.utf8 | 

Workspaces A,B,C and D all have a table called devices_device which contains an ID, a name and some other fields.

What function(s) do I need to call when saving a Device (over-writing the Django save() function) to make sure that a Device with the same parameters is not already present?

This is what I currently have outlined, with question marks where I don't know which function to use

def save(self, *args, **kwargs):
    for tab in ?.objects.all():
        if tab.object.using('devices_device')? == self.device_reference
        and ?.device_name == self.device_name
        and  ?.device_address == self.device_address
        and  ?.device_position== self.device_position
        and  ?.device_desciption == self.device_desciption:
            raise ValidationError(
                "This device already exists in another workspace!"
            )
    super().save(*args, **kwargs)

1 Answer 1

1

The obvious disclaimer first: Imposing unique constraints in code is a recipe for chaos and low performance. That's what RDBMS's are for (and you have several of them :).

That said, you can of course loop over every one of the databases where devices are stored and check for the presence of a matching device. For that to work, you have to include each database in your settings.DATABASES.

from django.conf import settings

for db in settings.DATABASES:
    if YourModel.objects.using(db).filter(
        # add your filter conditions here
    ).exists():
        raise ValidationError(
            "This device already exists in another workspace!"
        )            

I assume you have already read the helpful intro to multiple databases in the Django documentation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.