ct21
ct21

Reputation: 65

My database is not being created (flask-sqlalchemy)

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)

Upvotes: 1

Views: 2843

Answers (1)

ct21
ct21

Reputation: 65

Changing

SQLALCHEMY_DATABASE_URI = db_uri

to

app.config['SQLALCHEMY_DATABASE_URI'] = db_uri

fixed it.

Upvotes: 1

Related Questions