Home:ALL Converter>flask-sqlalchemy or sqlalchemy

flask-sqlalchemy or sqlalchemy

Ask Time:2013-01-16T01:51:56         Author:Amin

Json Formatter

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 motivations in http://packages.python.org/Flask-SQLAlchemy/index.html or maybe I did not understand the value!! I would appreciate your clarifications.

Author:Amin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/14343740/flask-sqlalchemy-or-sqlalchemy
MOCKBA :

To be honest, I don't see any benefits. IMHO, Flask-SQLAlchemy creates an additional layer you don't really need. In our case we have a fairly complex Flask application with multiple databases/connections (master-slave) using both ORM and Core where, among other things, we need to control our sessions / DB transactions (e.g. dryrun vs commit modes). Flask-SQLAlchemy adds some additional functionality such as automatic destruction of the session assuming some things for you which is very often not what you need.",
2013-03-04T03:18:24
schlamar :

The SQLAlchemy documentation clearly states that you should use Flask-SQLAlchemy (especially if you don't understand its benefits!): \n\n\n [...] products such as Flask-SQLAlchemy [...] SQLAlchemy strongly recommends that these products be used as available. \n\n\nThis quote and a detailed motivation you can find in the second question of the Session FAQ.",
2013-01-16T07:48:39
Mike Waites :

As @schlamar suggests, Flask-SqlAlchemy is definitely a good thing. I'd just like to add some extra context to the point made there.\nDon't feel like you are choosing one over the other. For example, let's say we want to grab all records from a table using a model using Flask-Sqlalchemy. It is as simple as\nModel.query.all()\n\nFor a lot of the simple cases, Flask-Sqlalchemy is going to be totally fine. The extra point that I would like to make is, if Flask-Sqlalchemy is not going to do what you want, then there's no reason you can't use SqlAlchemy directly.\nfrom myapp.database import db\n\nnum_foo = db.session.query(func.count(OtherModel.id)).filter(is_deleted=False).as_scalar()\n\ndb.session.query(Model.id, num_foo.label('num_foo')).order_by('num_foo').all()\n\nAs you can see, we can easily jump from one to the other with no trouble and in the second example we are in fact using the Flask-Sqlalchemy defined models.",
2014-01-06T20:46:09
Borys Serebrov :

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.\nThis 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.\nIn the ideal world that would be the only feature of Flask-SQLAlchemy, but actually, it adds few more things. Check out the docs for more info. Or see this blog post with the overview of them: Demystifying Flask-SQLAlchemy (update: the original article is not available at the moment, there is a snapshot on webarchive).\nWhen I first worked with Flask and SQLAlchemy, I didn't like this overhead . I went over and extracted the session management code from the extension. This approach works, although I discovered that it is quite difficult to do this integration properly.\nSo the easier approach (which is used in another project I am working on) is to just drop the Flask-SQLAlchemy in and don't use any of additional features it provides. You will have the db.session and you can use it as if it was pure SQLAlchemy setup.",
2016-11-13T20:27:50
Kimmo Hintikka :

Flask-SQLAlchemy gives you a number of nice extra's you would else end up implementing yourself using SQLAlchemy.\nPositive sides on using Flask-SQLAlchemy\n\n\nFlask_SQLAlchemy handles session configuration, setup and teardown for you.\nGives you declarative base model that makes querying and pagination easier\nBackend specific settings.Flask-SQLAlchemy scans installed libs for Unicode support and if fails automatically uses SQLAlchemy Unicode.\nHas a method called apply_driver_hacks that automatically sets sane defaults to thigs like MySQL pool-size\nHas nice build in methods create_all() and drop_all() for creating and dropping all tables. Useful for testing and in python command line if you did something stupid\nIt gives you get_or_404()instead of get() and find_or_404() instead of find() Code example at > http://flask-sqlalchemy.pocoo.org/2.1/queries/\n\nAutomatically set table names. Flask-SQLAlchemy automatically sets your table names converting your ClassName > class_name this can be overridden by setting __tablename__ class\nList item\nNegative sides on using Flask-SQLAlchemy\n\n\nUsing Flask-SQLAlchemy will make add additional difficulties to for\nmigrating from Flask to let's say Pyramid if you ever need to. This is mainly due to the custom declarative base model on Flask_SQLAchemy.\nUsing Flask-SQLAlchemy you risk using a package with a much smaller community than SQLAlchemy itself, which I cannot easily drop from active development any time soon.\nSome nice extras Flask-SQLAlchemy has can make you confused if you do not know they are there.\n",
2016-11-13T21:37:17
yy