Questions tagged [flask-sqlalchemy]

Flask-SQLALchemy is an extension for Flask that provides SQLAlchemy support. It is designed to make database manipulation through SQLAlchemy even easier and simpler. It has the same three clause BSD License as Flask.

Armin Ronacher has developed Flask-SQLAlchemy as well as Flask itself, Jinja and Werkzeug. All of those written in Python. In the extension project main page the autor states:

Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It aims to simplify using SQLAlchemy with Flask by providing useful defaults and extra helpers that make it easier to accomplish common tasks.

Flask-SQLAlchemy is open-source and hosted on Github

It's easy to setup and easier to use. You just have to install it via pip install Flask-SQLAlchemy or easy_install Flask-SQLAlchemy, and have the required software already installed.

After that you can test the installation and get a grasp of the basic usage with this quickstart tutorial.

7021 questions
330
votes
10 answers

How to execute raw SQL in Flask-SQLAlchemy app

How do you execute raw SQL in SQLAlchemy? I have a python web app that runs on flask and interfaces to the database through SQLAlchemy. I need a way to run the raw SQL. The query involves multiple table joins along with Inline views. I've…
starwing123
  • 3,413
  • 3
  • 13
  • 7
246
votes
7 answers

How to update SQLAlchemy row entry?

Assume table has three columns: username, password and no_of_logins. When user tries to login, it's checked for an entry with a query like user = User.query.filter_by(username=form.username.data).first() If password matches, he proceeds further.…
webminal.org
  • 44,948
  • 37
  • 94
  • 125
223
votes
4 answers

How to delete a record by id in Flask-SQLAlchemy

I have users table in my MySql database. This table has id, name and age fields. How can I delete some record by id? Now I use the following code: user = User.query.get(id) db.session.delete(user) db.session.commit() But I don't want to make any…
Sergey
  • 5,396
  • 3
  • 26
  • 38
209
votes
8 answers

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres

I'm trying to connect to a Postgres database with SQLAlchemy. I've installed psycopg2. However, I get the error sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres. How do I configure SQLAlchemy to connect to…
Maharsh Gheewala
  • 2,091
  • 2
  • 10
  • 6
206
votes
9 answers

Flask SQLAlchemy query, specify column names

How do I specify the column that I want in my query using a model (it selects all columns by default)? I know how to do this with the sqlalchmey session: session.query(self.col1), but how do I do it with with models? I can't do SomeModel.query().…
Matthew Scragg
  • 4,540
  • 3
  • 19
  • 27
191
votes
6 answers

How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?

Every time I run my app that uses Flask-SQLAlchemy I get the following warning that the SQLALCHEMY_TRACK_MODIFICATIONS option will be disabled. /home/david/.virtualenvs/flask-sqlalchemy/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:800:…
Robert
  • 39,162
  • 17
  • 99
  • 152
187
votes
9 answers

Flask-SQLalchemy update a row's information

How can I update a row's information? For example I'd like to alter the name column of the row that has the id 5.
pocorschi
  • 3,605
  • 5
  • 26
  • 35
174
votes
15 answers

SQLAlchemy ORM conversion to pandas DataFrame

Is there a solution converting a SQLAlchemy to a pandas DataFrame? Pandas has the capability to use pandas.read_sql but this requires use of raw SQL. I have two reasons for wanting to avoid it: I already have everything using the ORM…
Jared
  • 3,651
  • 11
  • 39
  • 64
151
votes
2 answers

Flask-SQLAlchemy import/context issue

I want to structure my Flask app something like: ./site.py ./apps/members/__init__.py ./apps/members/models.py apps.members is a Flask Blueprint. Now, in order to create the model classes I need to have a hold of the app, something like: #…
Brad Wright
  • 5,602
  • 6
  • 29
  • 30
146
votes
6 answers

Case Insensitive Flask-SQLAlchemy Query

I'm using Flask-SQLAlchemy to query from a database of users; however, while user = models.User.query.filter_by(username="ganye").first() will return doing user =…
Ganye
  • 2,481
  • 3
  • 16
  • 13
142
votes
15 answers

jsonify a SQLAlchemy result set in Flask

I'm trying to jsonify a SQLAlchemy result set in Flask/Python. The Flask mailing list suggested the following method http://librelist.com/browser//flask/2011/2/16/jsonify-sqlalchemy-pagination-collection-result/#04a0754b63387f87e59dda564bde426e…
mal-wan
  • 4,391
  • 4
  • 26
  • 37
142
votes
6 answers

flask-sqlalchemy or sqlalchemy

I am new in both flask and sqlalchemy, I just start working on a flask app, and I am using sqlalchemy for now. I was wondering if there is any significant benefit I can get from using flask-sqlalchemy vs sqlalchemy. I could not find enough…
Amin
  • 1,883
  • 4
  • 17
  • 22
138
votes
5 answers

Flask-SQLAlchemy how to delete all rows in a single table

How do I delete all rows in a single table using Flask-SQLAlchemy? Looking for something like this: >>> users = models.User.query.all() >>> models.db.session.delete(users) # but it errs out: UnmappedInstanceError: Class '__builtin__.list' is not…
SeanPlusPlus
  • 8,663
  • 18
  • 59
  • 84
127
votes
3 answers

SQLAlchemy create_all() does not create tables

I'm trying to integrate PostgreSQL and SQLAlchemy but SQLAlchemy.create_all() is not creating any tables from my models. My code: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app =…
Paul
  • 6,641
  • 8
  • 41
  • 56
114
votes
1 answer

What is the difference between the declarative_base() and db.Model?

The quickstart tutorial for the Flask-SQLAlchemy plugin instructs users to create table models inheriting the db.Model class, e.g. app = Flask(__main__) db = SQLAlchemy(app) class Users(db.Model): __tablename__ = 'users' ... However, the…
drs
  • 5,679
  • 4
  • 42
  • 67
1
2 3
99 100