librelist archives

« back to archive

The purpose of Flask-SQLAlchemy

The purpose of Flask-SQLAlchemy

From:
Philip Goh
Date:
2012-11-07 @ 09:36
Dear list,

I've been having a look at Flask-SQLAlchemy, and I am struggling to see why
I would use this extension instead of just using SQLAlchemy directly? The
example provided can be easily done in pure SQLAlchemy, using the
declarative extension.

What does this extension add?

Thanks,
Philip

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Edd Robinson
Date:
2012-11-07 @ 09:51
Hi Philip,

there are a couple of neat features that Flask-SQLAlchemy adds. They are 
explained in the documentation.

Some of the useful ones (for me at least):

- Pagination object, which is a nice helper object for paginating through 
db results (and makes it simple to integrate with front-end pagination).

- get_debug_queries() easy inspection and checking of SQL queries 
available while the request context is around.

Of course you can do all this with Vanilla SQL-A, but I find the Flask 
extension useful.

Cheers,
Edd



On 7 Nov 2012, at 09:36, Philip Goh wrote:

> Dear list,
> 
> I've been having a look at Flask-SQLAlchemy, and I am struggling to see 
why I would use this extension instead of just using SQLAlchemy directly? 
The example provided can be easily done in pure SQLAlchemy, using the 
declarative extension.
> 
> What does this extension add?
> 
> Thanks,
> Philip
> 

Re: The purpose of Flask-SQLAlchemy

From:
Philip Goh
Date:
2012-11-07 @ 13:16
Thanks all for the discussion so far. I thought I'd reply to my own
original post instead of replying to everyone individually.

Much appreciated,
Philip


On 7 November 2012 09:36, Philip Goh <philip.wj.goh@gmail.com> wrote:

> Dear list,
>
> I've been having a look at Flask-SQLAlchemy, and I am struggling to see
> why I would use this extension instead of just using SQLAlchemy directly?
> The example provided can be easily done in pure SQLAlchemy, using the
> declarative extension.
>
> What does this extension add?
>
> Thanks,
> Philip
>
>

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Serge S. Koval
Date:
2012-11-07 @ 09:49
It is just thin wrapper around SQLAlchemy with some Flask integration
helpers and tools which allow query introspection. Flask-DebugToolbar uses
them to populate SQL pane.

It is up for you which one to use. In project that involves code sharing
between different frameworks (it was Flask and Tornado in my case), I'm
using pure SQLAlchemy. For Flask-only project, I'm using Flask-SQLAlchemy.

Serge.

On Wed, Nov 7, 2012 at 11:36 AM, Philip Goh <philip.wj.goh@gmail.com> wrote:

> Dear list,
>
> I've been having a look at Flask-SQLAlchemy, and I am struggling to see
> why I would use this extension instead of just using SQLAlchemy directly?
> The example provided can be easily done in pure SQLAlchemy, using the
> declarative extension.
>
> What does this extension add?
>
> Thanks,
> Philip
>
>

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Jeffrey Tratner
Date:
2012-11-07 @ 09:58
Flask-SQLAlchemy also makes it easier to handle connection pooling and the
request lifecycle.


On Wed, Nov 7, 2012 at 4:49 AM, Serge S. Koval <serge.koval@gmail.com>wrote:

> It is just thin wrapper around SQLAlchemy with some Flask integration
> helpers and tools which allow query introspection. Flask-DebugToolbar uses
> them to populate SQL pane.
>
> It is up for you which one to use. In project that involves code sharing
> between different frameworks (it was Flask and Tornado in my case), I'm
> using pure SQLAlchemy. For Flask-only project, I'm using Flask-SQLAlchemy.
>
> Serge.
>
>
> On Wed, Nov 7, 2012 at 11:36 AM, Philip Goh <philip.wj.goh@gmail.com>wrote:
>
>> Dear list,
>>
>> I've been having a look at Flask-SQLAlchemy, and I am struggling to see
>> why I would use this extension instead of just using SQLAlchemy directly?
>> The example provided can be easily done in pure SQLAlchemy, using the
>> declarative extension.
>>
>> What does this extension add?
>>
>> Thanks,
>> Philip
>>
>>
>

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Philip Goh
Date:
2012-11-07 @ 13:14
On 7 November 2012 09:58, Jeffrey Tratner <jtratner@gmail.com> wrote:

> Flask-SQLAlchemy also makes it easier to handle connection pooling and the
> request lifecycle.
>
>
Connection pooling is straightforward in SQLAlchemy as it's a property of
the engine.

Handling the request life cycle is interesting, as I do not know the best
approach. Do you create a session per request? How does Flask-SQLAlchemy
handle it?

Cheers,
Philip

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Teo Klestrup Röijezon
Date:
2012-11-07 @ 13:45
I'm a bit late, but the way Flask-SQLAlchemy does it is that it creates a
session for each request, yes, and then binds it to the current request. It
then aborts the session during the request teardown, which will cause all
changes not manually committed to be lost.


On 7 November 2012 14:14, Philip Goh <philip.wj.goh@gmail.com> wrote:

> On 7 November 2012 09:58, Jeffrey Tratner <jtratner@gmail.com> wrote:
>
>> Flask-SQLAlchemy also makes it easier to handle connection pooling and
>> the request lifecycle.
>>
>>
> Connection pooling is straightforward in SQLAlchemy as it's a property of
> the engine.
>
> Handling the request life cycle is interesting, as I do not know the best
> approach. Do you create a session per request? How does Flask-SQLAlchemy
> handle it?
>
> Cheers,
> Philip
>
>
>
>
>
>

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Philip Goh
Date:
2012-11-07 @ 13:56
On 7 November 2012 13:45, Teo Klestrup Röijezon <teo@nullable.se> wrote:

> I'm a bit late, but the way Flask-SQLAlchemy does it is that it creates a
> session for each request, yes, and then binds it to the current request. It
> then aborts the session during the request teardown, which will cause all
> changes not manually committed to be lost.
>
>
> I'm doing something similar, so this is good news.

It's looking more and more obvious that I should avoid Flask-SQLAlchemy for
my current project as I already have written most of what it provides. I'll
revisit it in a new project.

Cheers,
Philip

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Owein Reese
Date:
2012-11-07 @ 14:53
Hold on, you want to avoid a tried and true library that the community has
vetted because you've pretty much written your own version? Isn't it better
to use something kid tested and mother approved? I generally stay quiet on
these sorts of things until I hear something like that.
On Nov 7, 2012 9:05 AM, "Philip Goh" <philip.wj.goh@gmail.com> wrote:

> On 7 November 2012 13:45, Teo Klestrup Röijezon <teo@nullable.se> wrote:
>
>> I'm a bit late, but the way Flask-SQLAlchemy does it is that it creates a
>> session for each request, yes, and then binds it to the current request. It
>> then aborts the session during the request teardown, which will cause all
>> changes not manually committed to be lost.
>>
>>
>> I'm doing something similar, so this is good news.
>
> It's looking more and more obvious that I should avoid Flask-SQLAlchemy
> for my current project as I already have written most of what it provides.
> I'll revisit it in a new project.
>
> Cheers,
> Philip
>
>

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Simon Sapin
Date:
2012-11-07 @ 17:19
Le 07/11/2012 15:53, Owein Reese a écrit :
> Hold on, you want to avoid a tried and true library that the community
> has vetted because you've pretty much written your own version? Isn't it
> better to use something kid tested and mother approved? I generally stay
> quiet on these sorts of things until I hear something like that.

I agree that Flask-SQLAlchemy is nice to get started quickly, but you 
can easily "grow out of it". It’s really not mandatory, bare SQLAlchemy 
works really well.

-- 
Simon Sapin

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Jeffrey Tratner
Date:
2012-11-07 @ 20:58
Not sure if this is still the case, but I encountered some problems using
it *outside* of a flask session. (can't remember exactly what it was).  I
ended up conditionally importing the SQLAlchemy classes/functions I needed
to use, depending on whether I was using a flask session or loading up data
behind the scenes.


On Wed, Nov 7, 2012 at 12:19 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 07/11/2012 15:53, Owein Reese a écrit :
> > Hold on, you want to avoid a tried and true library that the community
> > has vetted because you've pretty much written your own version? Isn't it
> > better to use something kid tested and mother approved? I generally stay
> > quiet on these sorts of things until I hear something like that.
>
> I agree that Flask-SQLAlchemy is nice to get started quickly, but you
> can easily "grow out of it". It’s really not mandatory, bare SQLAlchemy
> works really well.
>
> --
> Simon Sapin
>

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Philip Goh
Date:
2012-11-08 @ 09:05
On 7 Nov 2012, at 14:53, Owein Reese <owreese@gmail.com> wrote:

> Hold on, you want to avoid a tried and true library that the community 
has vetted because you've pretty much written your own version? Isn't it 
better to use something kid tested and mother approved? I generally stay 
quiet on these sorts of things until I hear something like that.
> 

This applies to new projects.

However, if I have code that I've written that has been tested and is 
running fine why would I swap it out for something that is more popular 
but untested in my current setup? Especially if it doesn't give me any 
perceivable benefit?

Horses for courses.

Cheers,
Philip

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Owein Reese
Date:
2012-11-08 @ 13:08
I think we've all got those stories wherein we swap a well known library
for a home brew only to have all the bugs magically disappear. Gut
reaction. If it's working for you, great.

No disrespect intended. I just prefer to work with and have others around
me build upon known quantities even if the solution seems trivial because
it avoids subtle or innocuous bugs. That way if something is wrong you can
generally attribute it to your own code.
On Nov 8, 2012 4:11 AM, "Philip Goh" <philip.wj.goh@gmail.com> wrote:

>
> On 7 Nov 2012, at 14:53, Owein Reese <owreese@gmail.com> wrote:
>
> Hold on, you want to avoid a tried and true library that the community has
> vetted because you've pretty much written your own version? Isn't it better
> to use something kid tested and mother approved? I generally stay quiet on
> these sorts of things until I hear something like that.
>
> This applies to new projects.
>
> However, if I have code that I've written that has been tested and is
> running fine why would I swap it out for something that is more popular but
> untested in my current setup? Especially if it doesn't give me any
> perceivable benefit?
>
> Horses for courses.
>
> Cheers,
> Philip
>
>

Re: [flask] The purpose of Flask-SQLAlchemy

From:
Jon Parise
Date:
2012-11-07 @ 09:45
The Flask-SQLAlchemy documentation sums it up nicely:

    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.

If you don't find those defaults and helpers useful, then you probably
won't see a benefit from using the extension.  But many people do, and
that's why the extension is popular.

The best way to see the specific features is by looking at the
documentation and source code directly.

On Wed, Nov 7, 2012 at 1:36 AM, Philip Goh <philip.wj.goh@gmail.com> wrote:
> Dear list,
>
> I've been having a look at Flask-SQLAlchemy, and I am struggling to see why
> I would use this extension instead of just using SQLAlchemy directly? The
> example provided can be easily done in pure SQLAlchemy, using the
> declarative extension.
>
> What does this extension add?
>
> Thanks,
> Philip
>