Skip to content

Commit 2c3650a

Browse files
committed
Addressing docs note
1 parent 73e0206 commit 2c3650a

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

docs/quickstart.rst

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ Initialize the Extension
4444
------------------------
4545

4646
First create the ``db`` object using the ``SQLAlchemy`` constructor.
47+
The initialization step depends on which version of ``SQLAlchemy`` you're using.
48+
This extension supports both SQLAlchemy 1 and 2, but defaults to SQLAlchemy 1.
49+
50+
.. _sqlalchemy1-initialization:
51+
52+
Using the SQLAlchemy 1 API
53+
^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
55+
To use the SQLAlchemy 1.x API, you do not need to pass any arguments to the ``SQLAlchemy`` constructor.
4756

4857
.. code-block:: python
4958
@@ -53,10 +62,12 @@ First create the ``db`` object using the ``SQLAlchemy`` constructor.
5362
5463
db = SQLAlchemy()
5564
65+
.. _sqlalchemy2-initialization:
5666

57-
By default, this extension assumes that you are using the SQLAlchemy 1.x API for defining models.
67+
Using the SQLAlchemy 2 API
68+
^^^^^^^^^^^^^^^^^^^^^^^^^^
5869

59-
To use the new SQLAlchemy 2.x API, pass a subclass of either ``DeclarativeBase`` or ``DeclarativeBaseNoMeta``
70+
To use the new SQLAlchemy 2.x API, pass a subclass of either `DeclarativeBase`_ or `DeclarativeBaseNoMeta`_
6071
to the constructor.
6172

6273
.. code-block:: python
@@ -70,6 +81,25 @@ to the constructor.
7081
7182
db = SQLAlchemy(model_class=Base)
7283
84+
If desired, you can enable `SQLAlchemy's native support for data classes`_
85+
by adding `MappedAsDataclass` as an additional parent class.
86+
87+
.. code-block:: python
88+
89+
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
90+
91+
class Base(DeclarativeBase, MappedAsDataclass):
92+
pass
93+
94+
db = SQLAlchemy(model_class=Base)
95+
96+
.. _DeclarativeBase: https://docs.sqlalchemy.org/en/20/orm/mapping_api.html#sqlalchemy.orm.DeclarativeBase
97+
.. _DeclarativeBaseNoMeta: https://docs.sqlalchemy.org/en/20/orm/mapping_api.html#sqlalchemy.orm.DeclarativeBaseNoMeta
98+
.. _SQLAlchemy's native support for data classes: https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#native-support-for-dataclasses-mapped-as-orm-models
99+
100+
About the ``SQLAlchemy`` object
101+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102+
73103
Once constructed, the ``db`` object gives you access to the :attr:`db.Model <.SQLAlchemy.Model>` class to
74104
define models, and the :attr:`db.session <.SQLAlchemy.session>` to execute queries.
75105

@@ -122,7 +152,7 @@ The table name ``"user"`` will automatically be assigned to the model's table.
122152

123153
It's also possible to use the SQLAlchemy 2.x style of defining models,
124154
as long as you initialized the extension with an appropriate 2.x model base class
125-
as described above.
155+
as described in :ref:`sqlalchemy2-initialization`.
126156

127157
.. code-block:: python
128158

0 commit comments

Comments
 (0)