Disentangling the ReactPy-Django Cache/DB #152
-
I've set REACTPY_CACHE = "local_reactpy"
REACTPY_DATABASE = "local_reactpy" to a DB that is not the default one but had several issues (starting with being unable to 'migrate reactpy_django' to 'local_reactpy' but I'm not sure what went wrong there [*]). One of the issues came from here. According to GPT-4: Django's full_clean() method runs all the field’s clean methods. If all the clean methods are run successfully, it will then run the validate_unique() method on your model to check any uniqueness constraints. Now, the validate_unique() method requires a database lookup to see if the unique constraint is violated, and by default, it uses the DEFAULT database alias. That's why you are seeing an SQL query being executed when you call full_clean(). To specify the database for all operations including the full_clean(), you need to override the validate_unique() method in your model, so that it uses the specific database that you want: class ComponentSession(models.Model):
...
def validate_unique(self, exclude=None):
qs = self.__class__.objects.using(REACTPY_DATABASE).filter(uuid=self.uuid)
if qs.exists():
raise ValidationError("UUID must be unique.")
... === [*] P.S. Initial migration worked this way: ./manage migrate --database "local_reactpy" auth
./manage migrate --database "local_reactpy" reactpy_django |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
I wasn't able to replicate any issues with I'm going to need some exception stacks for this, or perhaps you could share a repo where this issue can be reproduced. |
Beta Was this translation helpful? Give feedback.
I've developed a database router which may help in your scenario. Please let me know if it works.
pip install git+https://github.com/Archmonger/reactpy-django.git@dj-db-routing
DATABASE_ROUTERS = ['reactpy_django.database.Router']