Comments (11)
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.
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.
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.
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.
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.
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.
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.
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.
Ah I see, thanks for clarifying this. I think we can add this to SA-Utils then.
from sqlalchemy-utils.
ping
from sqlalchemy-utils.
Closing due to inactivity.
from sqlalchemy-utils.
Related Issues (20)
- FeatReq: Add the DynamicTable class
- Unable to call refresh_materialized_view on CRDB w/SQLAlchemy 2.0 HOT 1
- Create SECURITY.md
- CompositeType support for pg8000 and psycopg3 HOT 1
- create_view not accepting `replace` argument HOT 2
- Helper functions for explicit table locks ?
- custom aggregated column does not work with nullable=False
- Database_exist not working in python 3.11 HOT 1
- Even though binary was set false, impl remain binary(16)
- generic_relationship breaks with SqlAlchemy 2.0.22 HOT 3
- [Feature] Add MySQL collation support in `create_database`
- DeprecationWarning: 'crypt' is deprecated HOT 1
- Issue with change of implementation of EncryptedType from ‘LargeBinary’ to ‘String’ HOT 1
- [Feature] Enable Cascade Behavior for generic_relationship
- [Question] how to encrypt using aws kms in sqlalchemy
- Error on model with `server_default` for `DateTime` column and `UUIDType` primary key.
- [Feature] database helpers functions don't support custom create_engine()
- New release for SQLAlchemy >= 2.0.22 HOT 19
- Issue updating aggregate when setting foreign key to Null HOT 1
- Password checking error when using SQLAlchemy and Postgres
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sqlalchemy-utils.