How to save a specific Django application in another Postgresql database

I recently downloaded the django_messages application (a private user for messaging with the user's django application) and added it to my django project.

settings.py

INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'mydjangoapp.mydjangoappdb', 'tastypie', 'gunicorn', 'south', 'relationships', 'pyapns', 'django_messages', 

The app works great and works great with Django. However, for features such as messaging, the database can be quite large. I decided to create a special database to store all the django_messages data.

settings.py

 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'django_db', 'USER': 'django_login', 'PASSWORD': 'xxxx', 'HOST': '', 'PORT': '', }, 'message_db': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'django_messagedb', 'USER': 'django_login', 'PASSWORD': 'xxxx', 'HOST': 'XX.XXX.XX.XXX', 'PORT': '5432', } DATABASE_ROUTERS = ['mydjangoapp.messagerouter.MessageRouter'] 

And just for clarification, here is my messagerouter.py

 class MessageRouter(object): """ A custom router written by Riegie to control all database operations on models in the django_messages application """ def db_for_read(self, model, **hints): """ Attempts to read django_messages models go to message_db. """ if model._meta.app_label == 'django_messages': return 'message_db' return None def db_for_write(self, model, **hints): """ Attempts to write django_messages models to go to message_db. """ if model._meta.app_label == 'django_messages': return 'message_db' return None def allow_relation(self, obj1, obj2, **hints): """ Allow relations if a model in the django_messages. """ if obj1._meta.app_label == 'django_messages' or \ obj2._meta.app_label == 'django_messages': return True return None def allow_syncdb(self, db, model): """ Make sure the django_messages app only appears in the 'message_db" database. """ if db == 'message_db': return model._meta.app_label == 'django_messages' elif model._meta.app_label == 'django_messages': return False return None 

As you can see, I have two databases: one on the local machine running Django, and the other database on the remote machine. Out of the box, after installation, django_messages naturally creates tables in the default database. However, I would like it to create tables only in the message_db database.

I looked through the Multi-db Setup Django Documentation, but it details the configuration of the Master / Slave. I used the Auth Router example and created messagerouter.py. Everything is synchronized and I get no errors.

enter image description here

However, when I check the remote database, the table is not there! Why is this? Is this because it is not possible for a Django user to have a relation of a remote foreign key to a table?


UPDATE

Therefore, I was able to synchronize the Django_messages application with another database using the following command: ./manage.py syncdb --database = message_db . Excellent. However, when I access the application from the Django admin page, I get the following error:

 DatabaseError at /admin/django_messages/message/ relation "django_messages_message" does not exist LINE 1: SELECT COUNT(*) FROM "django_messages_message" 

I find this error odd because I see a table on another server through pgadmin III. So the synchronization worked correctly, but now it seems that Django cannot recognize this table. Am I something wrong with my messagerouter.py maybe?

+4
source share
2 answers

Therefore, having done a lot of research, I finally came across this, I am sorry that I have not seen it before. Django does not support relationships between databases: https://docs.djangoproject.com/en/dev/topics/db/multi-db/#no-cross-database-relations

As said: Django does not currently support foreign key or many-to-many relationships spanning multiple databases. If you used a router to separate models into different databases, any foreign keys and many-to-many relationships defined by these models must be internal to the same database.

This is due to referential integrity. In order to maintain communication between the two objects, Django must know that the primary key of the associated object is valid. If the primary key is stored in a separate database, it cannot be easily evaluated for the reliability of the primary key.

If you use Postgres, Oracle or MySQL with InnoDB, this is ensured at the database integrity level - database-level keyword restrictions prevent relationships that cannot be verified.

However, if you use SQLite or MySQL with MyISAM tables, there is no forced referential integrity; as a result, you can "fake foreign keys of an external database." However, this configuration is not officially supported by Django.

Hope this answer saves many of you.

+2
source

I also found this Django patch: https://code.djangoproject.com/attachment/ticket/17875/foreignkey-db-using.patch this can help with cross-databases

+2
source

All Articles