Multiple databases connections

Hey, so, I got an interesting request.

Context:

  • We have multiple MSSQL databases in different servers (16 servers, 1 db each)
  • We need to execute stored_procedures on those MSSQL dbs multiple times a day on demand with different parameters
  • We need to do it through a web interface and through endpoint calls

So, I was thinking using django because it’s easy to handle Auth, REST APIs and templates, but still not sure if the default DATABASES in the settings will be useful or not.

My approaches so far are:

  1. Use django for Auth and API purposes, along pyodbc to connect and execute the sprocs in those MSSQL dbs
  2. Same as 1 for Auth and API, but making independent services in flask to execute the sprocs for each server
  3. Understand if I can use the settings DATABASES without the need of running any migration on those.

Any suggestion is much appreciated!

Django supports multiple databases very well. See Multiple databases | Django documentation | Django.

You do not ever need to run migrations on any database. It’s never a required operation. It only exists to make some (most?) changes easier to manage. If you don’t want Django to manage a database, then don’t.

(We regularly integrate external data sources into our Django projects. It works quite well.)

1 Like

Thank you Ken, I will run some tests in the staging env so I can confirm this will work for me.

Update:
Just found 2 connectors, but both have issues. Django-mssql doesn’t seem to work from a Unix server and django-pyodbc is pretty deprecated, doesn’t work in django 3 or newer.

See the complete thread at Django3.0+ connects to SQL Server

It may also help to read Install the Microsoft ODBC driver for SQL Server (Linux) - ODBC Driver for SQL Server | Microsoft Docs

Was trying with django-mssql-backend and it doesn’t throw any error! that’s a good progress! thank you for that!

Now I was trying to access that specific db to run the stored procedure… from the documentation, it says to use django.db.connection but I’m not sure how it will choose the connection… django-mssql-doc

I know this is a little off-topic but you really look like an expert on this… Since I’m going to have 16 dbs configured in the settings, is there a way to pick a specific db connection to run my stored procedure?

Everything I know about multiple databases is located in the docs.

Without seeing what you’re trying to do, it would be difficult for me to offer any specific advice.

Thank you Ken, but that documentation is to use the ORM, in my case, I will not be dealing with it.
Still, found that db.connections.databases and db.connections.all() contain all the information I need.

Let me thank you one more time, your inputs were a great help to me in solving this.

Not all of it. There’s a section in there for how to use the connections object to get a raw cursor for direct database access. (Using raw cursors with multiple databases)

You are the best!!
Thank you!