On Mon, Mar 16, 2015 at 1:41 PM, Wolf Reitsamer
<wolf.reitsa...@gmail.com> wrote:
> Hi everyone,
>
> I can't get the following straight although I guess it's a pretty obvious
> thing, since I haven't found much about this topic online.
>
> I wrote a little python/flask/sqlalchemy based web-service where I want
> users to register themselves to post something. I also use flask-admin,
> LoginManager, etc. for this reason.
>
> Now as far as I understand I want to use a scoped_session, since I have a
> connection pool to my database and users could edit the database
> simultaneously.
>
> For that reason, I need my UserModel to be a subclass of Base (which is
> created using 'declarative_base').
>
> If I do so however, flask-user complains that my 'UserModel' has no
> attribute 'query' (in db_adapters.py line 85).
>
> What am I missing?
>

(This is guesswork since I've never used Flask)

In SQLAlchemy, scoped sessions and the declarative base system are
orthogonal features with no dependencies between them. You can use
declarative classes with non-scoped sessions, and scoped sessions with
non-declarative classes.

In a web application, you typically want to ensure that session aren't
shared between threads, and this is often implemented by using scoped
sessions. This is convenient because it allows you to refer to a
global Session object. It's not the only way to do it though - if you
are happy to pass the session as a parameter everywhere you need it,
it's perfectly reasonable to construct a non-scoped session at the
beginning of a request, and clean it up at the end of the request.

However, I guess you are using the Flask-SQLAlchemy package, which
makes a lot of assumptions for you. In particular, it gives you a
special base class that adds a "query" property to every subclass, so
that instead of writing:

    user = dbsession.query(User).get(1)

...you can write:

    user = User.query.get(1)

https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py#L729

https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py#L452

It sounds like flask-user assumes that you are using
Flask-SQLAlchemy's special declarative_base class. I think that as
long as you use Flask-SQLAlchemy's session (which is a scoped_session)
and declarative_base everywhere, you will probably be fine.

Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to