Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Disentangling the ReactPy-Django Cache/DB #152

Answered by Archmonger
numpde asked this question in Problem
Discussion options

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
You must be logged in to vote

I've developed a database router which may help in your scenario. Please let me know if it works.

  1. Install my branch pip install git+https://github.com/Archmonger/reactpy-django.git@dj-db-routing
  2. Add the database router DATABASE_ROUTERS = ['reactpy_django.database.Router']

Replies: 1 comment 3 replies

Comment options

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.

You must be logged in to vote
3 replies
Comment options

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.

Comment options

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.

Comment options

I've developed a database router which may help in your scenario. Please let me know if it works.

  1. Install my branch pip install git+https://github.com/Archmonger/reactpy-django.git@dj-db-routing
  2. Add the database router DATABASE_ROUTERS = ['reactpy_django.database.Router']
Answer selected by Archmonger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /