开发者

Django multiple databases - can't connect to mysql server fallback to sqlite

开发者 https://www.devze.com 2023-02-18 14:40 出处:网络
I have a django appli开发者_开发问答caiton with multiple databases.The default database is on the local machine.There is also a remote mysql database which is used for some write operations, but it is

I have a django appli开发者_开发问答caiton with multiple databases. The default database is on the local machine. There is also a remote mysql database which is used for some write operations, but it is not always up. When the server is down, mysqldb raises an OperationalError.

I would like have a local sqlite database called 'fallback' which would accept the data if the mysql server is down. I realize that this involves at try/except clause in django.db.mysql.base, but I am not quite sure where to go from there. Has anyone tried something similar? Do you have suggestions on a better way to handle this?


You could probably use Database Routers in combination with a custom base Model class that overrides the save method. Wrap it in a try..catch, and if the OperationalError occurs, provide some hints so your database router can determine if the fallback needs to be used.

I think this will be the cleanest way, rather than modifying the django code itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消