Giter Club home page Giter Club logo

Comments (11)

kvesteri avatar kvesteri commented on July 29, 2024

I think this could mimic the syntax of Flask-SQLAlchemy.

class UserQuery(Query):
    def filter_by_email(cls, query, email):
        return query.filter(# [ ... 

class User(Base):
   __query_cls__ = UserQuery

Using this kind of syntax allows us to make some neat query mixins as well. Also it keeps to code cleaner as the query modifiers are within the query class and do not bloat the model class.

As for the custom object creator methods you could just use custom classmethods:

>>> user = User.create_superuser('bob', password='b')

from sqlalchemy-utils.

mehcode avatar mehcode commented on July 29, 2024

There's two big differences I've seen with that approach. Flask-sqlalchemy replaces what comes after the session.query(Model) with the specified query class (I don't know what it does for joins). https://github.com/d1ffuz0r/sqlalchemy-manager merges methods into the existing query class.

Any preference? Mine would be on the merge because then joins could work (where a joined query expression would have methods from both?).

from sqlalchemy-utils.

kvesteri avatar kvesteri commented on July 29, 2024

Hmm I'm not sure I understand what you mean by Flask-SQLAlchemy replacing session.query. It merely assigns query property for each model so that whenever user calls Model.query it returns the appropriate Query subclass. Flask-SQLAlchemy does not touch the schematics of session.query(Model).

I think we should go with the Flask-SQLAlchemy way. I really like the way of defining query subclasses and assigning them to models (I've created many query mixins too, which I've found very useful). Monkey-patching the main query class just feels wrong.

from sqlalchemy-utils.

mehcode avatar mehcode commented on July 29, 2024

Oh.. okay. I understand that a bit better now.

Let me explain what I was rambling about.

session.query(Model)
session.query(ModelA).join(ModelB)

The above can be hooked (by deriving from the base sqlalchemy query and providing that class to the engine on construction) and the hook is provided a list of entities (Model, ModelA, ModelB, etc.) that are in the query expression. It would be nice if the following worked.

class UserQuery:

    @classobject
    def filter_by_email(cls, query, email):
        # ...
        return query

class User(Base):

    __query__ = UserQuery


class TeamQuery:

    @classobject
    def filter_by_color(cls, query, value):
        # ...
        return query

class Team(Base):

    __query__ = TeamQuery


>>> q = session.query(User).join(Team)
>>> q.filter_by_email('[email protected]').filter_by_color('red').all()
[...]

from sqlalchemy-utils.

kvesteri avatar kvesteri commented on July 29, 2024

Having these kind of methods that rely on joined entities need much more sophisticated things from the implementation. I think they could be built with transformers: http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/hybrid.html#building-transformers

The thing is that things can become very complex once you start having for example multiple joins on same entity.

from sqlalchemy-utils.

mehcode avatar mehcode commented on July 29, 2024

After looking at this again I agree with you.

I've implemented something like this in my lib. If you like it I'll move it over here. It's basically turns the query object into a factory that proxies to a declared query class on the model.

from sqlalchemy_utils import Query

class UserQuery(Query)

    def filter_by_email(self, email):
        return query.filter_by(email=email)

class User(Base):

    __query__ = UserQuery

>>> session.query(User)
<UserQuery()>

>>> session.query(User).filter_by_email("[email protected]").all()
[...]

See: https://github.com/concordusapps/alchemist/blob/master/src/alchemist/db/query.py

from sqlalchemy-utils.

kvesteri avatar kvesteri commented on July 29, 2024

I would love to see this working the same way Flask-SQLAlchemy works. So that you could do:

User.query.filter_by_email('[email protected]')

from sqlalchemy-utils.

mehcode avatar mehcode commented on July 29, 2024

I would love to see this working the same way Flask-SQLAlchemy works. So that you could do:

That's how it works in alchemist. See: https://github.com/concordusapps/alchemist/blob/master/alchemist/db/model.py#L98-L109

I can see moving the base Query class that I referenced above into sqlalchemy-utils however that requires some concept of a scoped/global session object. This is part of the reason I made sure to separate the two concepts and get session.query(User).filter_by_email to work.

from sqlalchemy-utils.

kvesteri avatar kvesteri commented on July 29, 2024

Ah I see, thanks for clarifying this. I think we can add this to SA-Utils then.

from sqlalchemy-utils.

matrixise avatar matrixise commented on July 29, 2024

ping

from sqlalchemy-utils.

kurtmckee avatar kurtmckee commented on July 29, 2024

Closing due to inactivity.

from sqlalchemy-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.