Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Multiple Databases - One not always available

I am developing a Django application which will use multiple database backends. I would like to put an sqlite database on the machine running the django application, and sync to a remote mysql database. The tricky part is that this machine running the application will not always have an internet connection, so the mysql database is not always availalble. There will be multiple machines running the application, each with it's own local sqlite DB, but all using the same remote mysql DB.

I haven't written the code yet, but here is what I have in mind. Every time I run an insert or update I would like to write it to both databases, unless the remote DB is unavailable, in which case I will save the sql statement in a table on the local database to run when the remote DB is available.

Can this be done with database routers, or do I need to manually implement this with each db statement?

Note on PK: Not directly related, but sure to be asked. The primary key will be generated locally on each machine. In the mysql DB there will be a field for this key, and a field with a unique identifier for each instance of the application, which together will provide a unique key.

like image 896
AgDude Avatar asked Feb 09 '11 13:02

AgDude


People also ask

Can you use multiple databases within the same Django project?

Django's admin doesn't have any explicit support for multiple databases. If you want to provide an admin interface for a model on a database other than that specified by your router chain, you'll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.

Which of these databases are not by default supported by Django?

What databases are supported by Django? PostgreSQL and MySQL, SQLite and Oracle. Apart from these, Django also supports databases such as ODBC, Microsoft SQL Server, IBM DB2, SAP SQL Anywhere, and Firebird using third-party packages. Note: Officially Django doesn't support any no-SQL databases.


2 Answers

Suppose you have a model called Blog then you can use the following to store it locally and remotely (assuming that you have configured access to the remote db).

blog = Blog('test') 
blog.save() #Assuming that sqlite is the default db
try:
    blog.save(using='mysql')
except NoInternetConnection:
    pass

Make sure you have defined and configured 'mysql' in settings.py and that you handle the cases when there is no Internet connection.

Side note: I am not quite sure why you actually would want to do this. If this is for backup purposes then I would use standard backup procedures. For more information about using multiple databases see: http://docs.djangoproject.com/en/dev/topics/db/multi-db/#using-raw-cursors-with-multiple-databases

like image 163
DrDee Avatar answered Oct 12 '22 09:10

DrDee


I took DrDee's code and attached it to the post_save signal (+1 for the help).

@receiver(models.signals.post_save) #new signal decorator in Django 1.3
def save_to_remote(sender,instance,using,**kwargs):
    if using == 'default' and instance.__module__ == 'mymodel.myapp.models':
        try:
            instance.save(using='remote')
        except:
            pending_instance=Pending_Remote(
                                            pk_default=instance.pk,
                                            model_default=instance.__class__.__name__
                                            )
            pending_instance.save()

This also saves a record of what was not saved to the remote database. Note that the model Pending_Remote must not be in 'myapp'.

like image 45
AgDude Avatar answered Oct 12 '22 08:10

AgDude