SQLAlchemy create_all() does not create tables

You should put your model class before create_all() call, like this: from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘postgresql+psycopg2://login:pass@localhost/flask_app’ db = SQLAlchemy(app) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) email = db.Column(db.String(120), unique=True) def __init__(self, username, email): self.username = username self.email = email def __repr__(self): return … Read more

flask-sqlalchemy or sqlalchemy

The main feature of the Flask-SQLAlchemy is proper integration with Flask application – it creates and configures engine, connection and session and configures it to work with the Flask app. This setup is quite complex as we need to create the scoped session and properly handle it according to the Flask application request/response life-cycle. In … Read more

jsonify a SQLAlchemy result set in Flask [duplicate]

It seems that you actually haven’t executed your query. Try following: return jsonify(json_list = qryresult.all()) [Edit]: Problem with jsonify is, that usually the objects cannot be jsonified automatically. Even Python’s datetime fails 😉 What I have done in the past, is adding an extra property (like serialize) to classes that need to be serialized. def … Read more

Flask-SQLAlchemy import/context issue

The flask_sqlalchemy module does not have to be initialized with the app right away – you can do this instead: # apps.members.models from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Member(db.Model): # fields here pass And then in your application setup you can call init_app: # apps.application.py from flask import Flask from apps.members.models import db … Read more

Flask-SQLalchemy update a row’s information

Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then, db.session.commit(). For example: admin = User.query.filter_by(username=”admin”).first() admin.email=”my_new_email@example.com” db.session.commit() user = User.query.get(5) user.name=”New Name” db.session.commit() Flask-SQLAlchemy is based on SQLAlchemy, so be sure to check out the SQLAlchemy Docs as … Read more

How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?

Most likely your application doesn’t use the Flask-SQLAlchemy event system, so you’re probably safe to turn off. You’ll need to audit the code to verify–you’re looking for anything that hooks into models_committed or before_models_committed. If you do find that you’re using the Flask-SQLAlchemy event system, you probably should update the code to use SQLAlchemy’s built-in … Read more