@@ -59,25 +59,28 @@ they are created or updated.
59
59
60
60
class TimestampModel (db .Model ):
61
61
__abstract__ = True
62
- created = db.Column (db.DateTime, nullable = False , default = datetime.utcnow)
63
- updated = db.Column (db.DateTime, onupdate = datetime.utcnow)
62
+ created: Mapped[datetime] = mapped_column (db.DateTime, nullable = False , default = datetime.utcnow)
63
+ updated: Mapped[datetime] = mapped_column (db.DateTime, default = datetime.utcnow , onupdate = datetime.utcnow)
64
64
65
65
class Author (db .Model ):
66
- ...
66
+ id : Mapped[int ] = mapped_column(db.Integer, primary_key = True )
67
+ username: Mapped[str ] = mapped_column(db.String, unique = True , nullable = False )
67
68
68
69
class Post (TimestampModel ):
69
- ...
70
+ id : Mapped[int ] = mapped_column(db.Integer, primary_key = True )
71
+ title: Mapped[str ] = mapped_column(db.String, nullable = False )
70
72
71
73
This can also be done with a mixin class, inheriting from ``db.Model `` separately.
72
74
73
75
.. code-block :: python
74
76
75
- class TimestampMixin :
76
- created = db.Column (db.DateTime, nullable = False , default = datetime.utcnow)
77
- updated = db.Column (db.DateTime, onupdate = datetime.utcnow)
77
+ class TimestampModel :
78
+ created: Mapped[datetime] = mapped_column (db.DateTime, nullable = False , default = datetime.utcnow)
79
+ updated: Mapped[datetime] = mapped_column (db.DateTime, default = datetime.utcnow , onupdate = datetime.utcnow)
78
80
79
- class Post (TimestampMixin , db .Model ):
80
- ...
81
+ class Post2 (TimestampModel , db .Model ):
82
+ id : Mapped[int ] = mapped_column(db.Integer, primary_key = True )
83
+ title: Mapped[str ] = mapped_column(db.String, nullable = False )
81
84
82
85
83
86
Session Class
@@ -168,14 +171,6 @@ on Flask-SQLAlchemy's detection and generation. The simple way to achieve that i
168
171
set each ``__tablename__ `` and not modify the base class. However, the table name
169
172
generation can be disabled by setting `disable_autonaming=True ` in the `SQLAlchemy ` constructor.
170
173
171
- Example code using the SQLAlchemy 1.x (legacy) API:
172
-
173
- .. code-block :: python
174
-
175
- db = SQLAlchemy(app, disable_autonaming = True )
176
-
177
- Example code using the SQLAlchemy 2.x declarative base:
178
-
179
174
.. code-block :: python
180
175
181
176
class Base (sa_orm .DeclarativeBase ):
0 commit comments