0

I followed this to setup with Windows authentication. Don't know if it works as there is an error right now.

Right now I have this:

from flask import Flask
from flask_sqlalchemy import sqlalchemy
app = Flask(__name__, static_url_path='', static_folder='static')
app.config['SQLACHLEMY_DATABASE_URI'] = 'mssql://LAPTOP-GNBBOVKT/SQLEXPRESS/OIS?trusted_connection=yes'
db = sqlalchemy(app)

class Country(db.Model):
    __tablename__ = 'example'
    id = db.Column('Country_ID', db.Integer, primary_key=True)
    code = db.Column('Country_Code', db.String)

@app.route('/test')
def test():
    allCountries = Country.query.all()
    return jsonify(allCountries)

But I'm getting the error:

TypeError: 'module' object is not callableatdb = sqlalchemy(app)

EDIT: Fixed the error by changing sqlalchemy to SQLAlchemy but now I'm getting

UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".

Probably because it's not connected to the database.

EDIT: What finally fixed it was importing pyodbc and changing URI line:

app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pyodbc://@' + 'LAPTOP-GNBBOVKT\\SQLEXPRESS' + '/' + 'OIS' + '?trusted_connection=yes&driver=ODBC+Driver+13+for+SQL+Server'
3
  • Are you using a username and password. Commented Apr 17, 2020 at 3:44
  • I want it to work with Windows Authentication as specified here: stackoverflow.com/a/24085353/12636797 Commented Apr 17, 2020 at 3:46
  • On the post you have mentioned check the comment by DoloMike on the accepted answer. Commented Apr 17, 2020 at 3:47

1 Answer 1

1

You should initialise the db object by db = SQLAlchemy ()

Also change your import to from flask_sqlaclhemy import SQLAlchemy

Edit: There is a typo Change to app.config['SQLALCHEMY_DATABASE_URI'] There's a typo

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

5 Comments

That fixed, the error, but the URI still seems to be wrong.
Edited answer. Pls check
Fixed the typo now I'm getting sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0)
Is there a specific reason why you want to use mssql. I am check the issue out
The data right now exists in an mssql db which is updated daily, so it's not up to me to chose the database.

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.