-1

I have a problem creating a database using flask, Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set; I always get this error

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

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///friends.db'

class Friends(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(200), nullable=False)
    date_created = db.Column(db.DateTime, default = datetime.utcnow)

    def __repr__(self):
            return '<Name %r>' % self.id
2

1 Answer 1

1

You need to move the db URI config to the line before instantiating a sqlalchemy object. Otherwise SQLAlchemy won't receive that data during instantiation.

Solution

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///friends.db' # Don't have this line below the next
db = SQLAlchemy(app)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.