Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify a custom query class #319

Closed
immunda opened this issue Jun 9, 2015 · 5 comments
Closed

Specify a custom query class #319

immunda opened this issue Jun 9, 2015 · 5 comments
Milestone

Comments

@immunda
Copy link
Contributor

immunda commented Jun 9, 2015

Devise a cohesive way to be able to provide a custom query class, which would be used for Model.query_class, session as qry_class, be set for db.Query, be used for db.relationship and db.backref. Essentially everywhere BaseQuery is used.

Thanks to the contributions and discussion from @justanr, @davidism & @RonnyPfannschmidt.

@RonnyPfannschmidt
Copy link

i dont see why #263 was not merged tho,

the next step would be to have a query_base parameter to init i suppose,

#263 was about db.session.query using a different query class than flask-sqlalchemy to begin with

@immunda
Copy link
Contributor Author

immunda commented Jun 9, 2015

Because I would prefer to pass it as an arg to the constructor and have that be reflected throughout, rather than accept it as part of sesssion_options and then change the API a second time.

@justanr
Copy link
Contributor

justanr commented Jun 11, 2015

Doing this properly will require passing the custom query class all the way down to _set_default_query_class, including _wrap_with_default_query_class and _include_sqlalchemy. Otherwise some queries will have the new query class and some will have the BaseQuery class.

The other issue is by default flask_sqlalchemy.Model declares query_class = BaseQuery explicitly. The only good work around is doing something like this in SQLAlchemy.__init__:

class SQLAlchemy(object):
     # may as well provide for custom base models as well
     # makes building something like SurrogatePKMixin easier
    def __init__(self, query_class=BaseQuery, model_class=Model, **others):
        self.Model = self.make_declarative_base(metadata, model_class)
        # not crazy about this part
        self.Model.query_class = query_class
        self.Query = query_class

@traut
Copy link

traut commented Aug 5, 2015

is that the last fix required for milestone 2.1?

@immunda
Copy link
Contributor Author

immunda commented Dec 4, 2015

Resolved in #328

@immunda immunda closed this as completed Dec 4, 2015
@davidism davidism modified the milestones: 2.1, v3.0 Jan 15, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

5 participants