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 {}
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.
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.
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 −
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With