You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If desired, you can enable `SQLAlchemy's native support for data classes`_
28
+
by adding `MappedAsDataclass` as an additional parent class.
20
29
21
30
.. code-block:: python
22
31
23
-
import sqlalchemyas sa
32
+
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass
24
33
25
-
classUser(db.Model):
26
-
id= sa.Column(sa.Integer, primary_key=True)
27
-
type= sa.Column(sa.String)
34
+
classBase(DeclarativeBase, MappedAsDataclass):
35
+
pass
36
+
37
+
.. _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
38
+
39
+
40
+
You can optionally construct the :class:`.SQLAlchemy` object with a custom
41
+
:class:`~sqlalchemy.schema.MetaData` object. This allows you to specify a custom
42
+
constraint `naming convention`_. This makes constraint names consistent and predictable,
43
+
useful when using migrations, as described by `Alembic`_.
44
+
45
+
.. code-block:: python
46
+
47
+
from sqlalchemy import MetaData
28
48
29
-
For convenience, the extension object provides access to names in the ``sqlalchemy`` and
30
-
``sqlalchemy.orm`` modules. So you can use ``db.Column`` instead of importing and using
31
-
``sqlalchemy.Column``, although the two are equivalent.
.. _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
70
100
71
About the ``SQLAlchemy`` object
101
72
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -134,34 +105,19 @@ keys are used.
134
105
Define Models
135
106
-------------
136
107
137
-
Subclass ``db.Model`` to define a model class. The ``db`` object makes the names in
138
-
``sqlalchemy`` and ``sqlalchemy.orm`` available for convenience, such as ``db.Column``.
108
+
Subclass ``db.Model`` to define a model class.
139
109
The model will generate a table name by converting the ``CamelCase`` class name to
140
110
``snake_case``.
141
111
142
-
This example uses the SQLAlchemy 1.x style of defining models:
0 commit comments