0

I have used postgresql with SQLAlchemy without issue but am trying to get at database that's in Azure running on SQL Server. I am unable to connect and can't really figure out why. I have searched quite a bit and have come across most articles pointing me towards using the create_engine but I can't seem to get that to work with using ORM. Here is my current code:

EXCEPTION

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:sqlserver

from flask import Flask
from models import db, People

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlserver://<connectionString>'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)

@app.route('/')
def hello_world():
    #Exception here
    people = People.query.filter_by(birthYear=12).all()
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

Azure offers me 4 different connection strings:

  • ADO.NET
  • JDBC
  • ODBC
  • PHP (not an option for me since i'm not using PHP)

Is there any way to make SQLAlchemy work with SQL Server?

https://docs.sqlalchemy.org/en/latest/dialects/mssql.html

1 Answer 1

1

SQLAlchemy is just a high level ORM library, the underlying communication to db is done by the dialect/DBAPI libraries.

Assume that you are planing to use pymssql, you need to install pymssql first, and modify the connection string

mssql+pymssql://<username>:<password>@<host>:<port>/<database_name>/?charset=utf8

Modern versions of this driver work very well with SQL Server and FreeTDS from Linux and is highly recommended.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, this is where I'm exploring now. I'm pretty new to Python so this could be an ignorent question, but how exactly do I install pymssql? Is it just through pip in my virtualenv?
Well, I think that got me a little further. Now my exception is a SqlOperationError for timeout. I'm assuming this means it just can't connect. For the freetds_name is that just my host?
Here is the link of Freetds, freetds.org. I guess you could try host:port/database_name if you are using windows. (I have not use it yet)
I'm on a mac. Do I need to setup anything for freetds? It's not clear what that actually does. The error that I'm dealing with says that it could be the result of the freetds.conf file not being found. So I'm guessing there is some setup to do with freetds. Also, is this just something to be handled locally or would I need to do the same setup in the production environment (azure app service)
It seems that Freetds is a little complicated, so do you want to use other dialects instead, e.g., pyodbc docs.sqlalchemy.org/en/latest/dialects/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.