flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Customize through `__init__` args

Open immunda opened this issue 9 years ago • 3 comments

As per the decision made in #357.

Functionality to customize by init args:

  • session options, including scope function
  • Session class (#130)
  • db.Query with query_class, defaults to BaseQuery
  • db.Model.metadata with metadata, defaults to None
  • db.Model base class (the base class of the declarative meta) with model_class

immunda avatar Oct 04 '16 21:10 immunda

Would this include options like the isolation_level? Edit: More of a curiosity question, I realized that isolation_level wasn't the fix to my problem.

stormbard avatar Nov 15 '16 02:11 stormbard

I want to be somewhat conservative with X_class arguments, as pairing that with X_options can get out of hand quickly. There's got to be some balance where having an X_class class attribute, or a factory method, along with X_options init arg makes sense for most use cases.

davidism avatar Mar 08 '19 17:03 davidism

Need that native session auto-committer. :)

zmarffy avatar Mar 23 '19 01:03 zmarffy

Implemented in #1087, although I think other earlier changes addressed most of this by adding the arguments. All setup methods are now considered internal.

  • session_options customizes the session. A class_ key can customize the session class. A scopefunc key can customize the scope function for the scoped session.
  • engine_options sets default options for all engines.
  • metadata sets the metadata for the default bind. When creating other metadata for other binds, the default metadata's naming_convention is copied.
  • query_class customizes the query class used by db.Query, db.Model.query, db.relationship(lazy="dynamic"), and db.sesison.query`. However, the query interface is legacy in SQLAlchemy 2.0.
  • model_class can be either a simple base class (sublcassing flask_sqlalchemy.model.Model), or a fully created declarative_base (using DefaultMeta or BindMetaMixin at least).

Between those, that seems to cover customizing all the objects managed by the extension. It otherwise shouldn't get in the way of the capabilities of SQLAlchemy.

davidism avatar Sep 18 '22 17:09 davidism