@@ -37,10 +37,10 @@ joined-table inheritance.
3737 db = SQLAlchemy(app, model_class = Base)
3838
3939 class User (db .Model ):
40- name: Mapped[str ] = mapped_column(String)
40+ name: Mapped[str ]
4141
4242 class Employee (User ):
43- title: Mapped[str ] = mapped_column(String)
43+ title: Mapped[str ]
4444
4545
4646 Abstract Models and Mixins
@@ -52,34 +52,33 @@ they are created or updated.
5252
5353.. code-block :: python
5454
55- from datetime import datetime
56- from sqlalchemy import DateTime, Integer, String
57- from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, declared_attr
55+ from datetime import datetime, timezone
56+ from sqlalchemy.orm import Mapped, mapped_column
5857
5958 class TimestampModel (db .Model ):
6059 __abstract__ = True
61- created: Mapped[datetime] = mapped_column(DateTime, nullable = False , default = datetime.utcnow )
62- updated: Mapped[datetime] = mapped_column(DateTime, default = datetime.utcnow , onupdate = datetime.utcnow )
60+ created: Mapped[datetime] = mapped_column(default = lambda : datetime.now(timezone.utc) )
61+ updated: Mapped[datetime] = mapped_column(default = lambda : datetime.now(timezone.utc) , onupdate = lambda : datetime.now(timezone.utc) )
6362
6463 class Author (db .Model ):
65- id : Mapped[int ] = mapped_column(Integer, primary_key = True )
66- username: Mapped[str ] = mapped_column(String, unique = True , nullable = False )
64+ id : Mapped[int ] = mapped_column(primary_key = True )
65+ username: Mapped[str ] = mapped_column(unique = True )
6766
6867 class Post (TimestampModel ):
69- id : Mapped[int ] = mapped_column(Integer, primary_key = True )
70- title: Mapped[str ] = mapped_column(String, nullable = False )
68+ id : Mapped[int ] = mapped_column(primary_key = True )
69+ title: Mapped[str ]
7170
7271 This can also be done with a mixin class, inheriting from ``db.Model `` separately.
7372
7473.. code-block :: python
7574
7675 class TimestampMixin :
77- created: Mapped[datetime] = mapped_column(DateTime, nullable = False , default = datetime.utcnow )
78- updated: Mapped[datetime] = mapped_column(DateTime, default = datetime.utcnow , onupdate = datetime.utcnow )
76+ created: Mapped[datetime] = mapped_column(default = lambda : datetime.now(timezone.utc) )
77+ updated: Mapped[datetime] = mapped_column(default = lambda : datetime.now(timezone.utc) , onupdate = lambda : datetime.now(timezone.utc) )
7978
8079 class Post (TimestampMixin , db .Model ):
81- id : Mapped[int ] = mapped_column(Integer, primary_key = True )
82- title: Mapped[str ] = mapped_column(String, nullable = False )
80+ id : Mapped[int ] = mapped_column(primary_key = True )
81+ title: Mapped[str ]
8382
8483
8584 Disabling Table Name Generation
0 commit comments