1

Basically I'm trying to make a Flask app using flask-sqlalchemy. I want to first load up a bunch of data into my database (database.py). However, I can't get this to work or even create the myapp.db file.

I'm getting no errors, however myapp.db file is not being created.

>>> db.engine
Engine(sqlite://)

The above should be myapp something, I'm pretty sure.

I have simplified my code to make it more concise to the problem.

Here I have my app:

from flask import Flask
import os
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

db_path = os.path.join(os.path.dirname(__file__), 'myapp.db')
db_uri = 'sqlite:///{}'.format(db_path)
SQLALCHEMY_DATABASE_URI = db_uri
db = SQLAlchemy(app)

from views import *


if __name__ == '__main__':
    app.secret_key = os.urandom(12)
    app.run(debug=True)

And I want to run my database.py to load all my data - which in my mind should be creating a myapp.db file??:

from flask_sqlalchemy import SQLAlchemy
import os
import re

from myapp import app
from models import *

## Creates our database
db.create_all()

username = test
password = test
user = User(username=username, password=password)

db.session.add(user)
db.session.commit()

Here is my models.py

from flask_sqlalchemy import SQLAlchemy
from myapp import app
# create a new SQLAlchemy object
db = SQLAlchemy(app)

class User(db.Model):

    """"""
    __tablename__ = "users"

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, nullable=False)
    password = db.Column(db.String, nullable=False)

1 Answer 1

1

Changing

SQLALCHEMY_DATABASE_URI = db_uri

to

app.config['SQLALCHEMY_DATABASE_URI'] = db_uri

fixed it.

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

1 Comment

Note that you should not create multiple separate db objects as you're doing. The db.Model defined in app will not for example contain the Declarative classes from models etc.

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.