Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

db.create_all() not creating tables in Flask-SQLAclchemy

I am trying to create a database table for a user class model using Flask-SQLAclchemy and Postgres.

My model class.

from app import db
class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(70), index=True, nullable=False)
    email = db.Column(db.String(70), index=True, unique=True, nullable=False)
    password = db.Column(db.String(128))

My app initialisation.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('app.instance.config.DevelopmentConfig')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:RandomPass@localhost/BrightEventDb'
SQLALCHEMY_TRACK_MODIFICATIONS = True
db = SQLAlchemy(app)

The following info is logged.

2018-01-06 11:53:09,978 INFO sqlalchemy.engine.base.Engine select version()
2018-01-06 11:53:09,979 INFO sqlalchemy.engine.base.Engine {}
2018-01-06 11:53:09,982 INFO sqlalchemy.engine.base.Engine select current_schema()
2018-01-06 11:53:09,982 INFO sqlalchemy.engine.base.Engine {}
2018-01-06 11:53:09,984 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
2018-01-06 11:53:09,985 INFO sqlalchemy.engine.base.Engine {}
2018-01-06 11:53:09,986 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
2018-01-06 11:53:09,987 INFO sqlalchemy.engine.base.Engine {}
2018-01-06 11:53:09,990 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
2018-01-06 11:53:09,991 INFO sqlalchemy.engine.base.Engine {}
like image 399
Felix Wambiri Avatar asked Jan 06 '18 09:01

Felix Wambiri


People also ask

What is flask-SQLAlchemy?

Flask-SQLAlchemy is a Flask extension that adds support for SQLAlchemy to a Flask app. SQLAlchemy is an Object Relational Mapper that basically allows us to treat SQL tables as python Classes and rows in those tables as instances of those Classes.

Should flask-SQLAlchemy default to a SQLite database?

The setting should be SQLALCHEMY_DATABASE_URI, not URL. You can see that the db doesn't have the right uri when you ran this line: >>> db <SQLAlchemy engine='sqlite://'> It shows that Flask-SQLAlchemy defaulted to an in-memory sqlite database.

How to check if students table is created in the database?

The college.db will be created in current working directory. To check if the students table is created, you can open the database using any SQLite GUI tool such as SQLiteStudio. The below image shows the students table that is created in the database −

How to check if a table is created using create_engine () function?

Because echo attribute of create_engine () function is set to True, the console will display the actual SQL query for table creation as follows − The college.db will be created in current working directory. To check if the students table is created, you can open the database using any SQLite GUI tool such as SQLiteStudio.


Video Answer


2 Answers

If you were following the flask quick start minimal application, the command worked by default since the User class was in the same place as the db instance. In your case, however, you will have to import the User class as mentioned in the comments from models import User into your app initialization

like image 117
Esir Kings Avatar answered Nov 15 '22 09:11

Esir Kings


As someone in the comments mentioned you are not importing your model into your app initialisation module. You could do that as follows

from models import User

like image 27
Zahir J Avatar answered Nov 15 '22 10:11

Zahir J