Home > Back-end >  Flask SQLALCHEMY_DATABASE_URI is not defined
Flask SQLALCHEMY_DATABASE_URI is not defined

Time:09-27

I am having a problem setting up the skeleton of my Flask API, where I am getting an error regarding the URI for my database.

I have it set up like this

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlite3 import Connection as SQLite3Connection
from datetime import datetime
from sqlalchemy import event
from sqlalchemy.engine import Engine
from flask import Flask, request, jsonify


app = Flask(__name__)

app.config[SQLALCHEMY_DATABASE_URI] = "sqlite:///sqlitedb.file"
app.config[SQLALCHEMY_TRACK_MODIFICATIONS] = 0

but I am getting the error in my IDE that [SQLALCHEMY_DATABASE_URI] is not defined and I also get the same error when trying to execute the program.

The modules import fine, so I'm not sure what I'm missing.

Edit: RESOLVED. I didn't have quotes around my config dictionary entries.

CodePudding user response:

From the docs:

The config is actually a subclass of a dictionary and can be modified just like any dictionary

this is how you modify app.config:

app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///sqlitedb.file"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = 0

CodePudding user response:

A crucial step is how SQLAlchemy gets informed of configuration. That'll look something like:

app = Flask(__name__)
# ... configuration
db = SQLAlchemy()
db.init_app(app)
  • Related