-
-
Notifications
You must be signed in to change notification settings - Fork 22
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.
All reactions
I've developed a database router which may help in your scenario. Please let me know if it works.
- Install my branch
pip install git+https://github.com/Archmonger/reactpy-django.git@dj-db-routing - Add the database router
DATABASE_ROUTERS = ['reactpy_django.database.Router']
Replies: 1 comment 3 replies
-
I wasn't able to replicate any issues with manage.py migrate reactpy_django.
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.
All reactions
-
The command ./manage.py migrate reactpy_django writes to my 'default' DB.
How could I use the following setup?
DATABASES = { # dedicated to reactpy_django 'reactpy': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', }, 'default': { # ... remote DB }, }
I can't do
./manage.py migrate reactpy_django --database=reactpy
because no such table: django_content_type in that DB, and even if I could it would get lost from the in-memory DB.
Beta Was this translation helpful? Give feedback.
All reactions
-
Are you using any DATABASE_ROUTERS? They may be impacting your migrations here.
Side note, you can't use an in-memory DB with ReactPy.
Beta Was this translation helpful? Give feedback.
All reactions
-
I've developed a database router which may help in your scenario. Please let me know if it works.
- Install my branch
pip install git+https://github.com/Archmonger/reactpy-django.git@dj-db-routing - Add the database router
DATABASE_ROUTERS = ['reactpy_django.database.Router']
Beta Was this translation helpful? Give feedback.