I have a django app that gets communication from a websocket.
To keep it short, I receive database commands via the websocket and they are all executed inside an atomic block.
Each user uses a different database connection (to the same database, but a different connection) in a different celery task.
The app works inside this atomic block and the users can modify the db without affecting the rest until they order the django app (the listener) to exit the block and commit.
All works very well, they can even use the default db connection (used for read only purposes) to make diffs of all the changes they have done.
The problem simplified is this:
Inside a with transaction.atomic(using=self.database_connection):
- delete a record.
- create the same record instance
- do record.full_clean() → triggers a unique constraint error.
record.save works fine though as the database (or the version for this connection at least) does not have the record.
I workarounded the problem by doing
record.clean_fields()
record.clean()
record.validate_unique()
Omitting the unique constraint check
I tried to create a manager and a router but due to my inexperience in Django (I guess) I got it to work exactly as using the default ones.