12
12
TIMESTAMP ,
13
13
VARCHAR ,
14
14
BigInteger ,
15
- Boolean ,
16
15
Column ,
17
- Date ,
18
- DateTime ,
16
+ Dialect ,
19
17
ForeignKey ,
20
18
Integer ,
21
19
SmallInteger ,
22
20
String ,
23
21
Table ,
24
22
Text ,
25
- Time ,
26
23
)
27
- from sqlalchemy .dialects .sqlite .base import SQLiteDialect
28
- from sqlalchemy .orm import backref , declarative_base , relationship
29
- from sqlalchemy .orm .decl_api import DeclarativeMeta
24
+ from sqlalchemy .orm import DeclarativeBase , Mapped , backref , mapped_column , relationship
30
25
from sqlalchemy .sql .functions import current_timestamp
31
26
32
27
33
28
class SQLiteNumeric (types .TypeDecorator ):
34
29
impl : t .Type [String ] = types .String
35
30
36
- def load_dialect_impl (self , dialect : SQLiteDialect ) -> t .Any :
31
+ def load_dialect_impl (self , dialect : Dialect ) -> t .Any :
37
32
return dialect .type_descriptor (types .VARCHAR (100 ))
38
33
39
- def process_bind_param (self , value : t .Any , dialect : SQLiteDialect ) -> str :
34
+ def process_bind_param (self , value : t .Any , dialect : Dialect ) -> str :
40
35
return str (value )
41
36
42
- def process_result_value (self , value : t .Any , dialect : SQLiteDialect ) -> Decimal :
37
+ def process_result_value (self , value : t .Any , dialect : Dialect ) -> Decimal :
43
38
return Decimal (value )
44
39
45
40
46
41
class MyCustomType (types .TypeDecorator ):
47
42
impl : t .Type [String ] = types .String
48
43
49
- def load_dialect_impl (self , dialect : SQLiteDialect ) -> t .Any :
44
+ def load_dialect_impl (self , dialect : Dialect ) -> t .Any :
50
45
return dialect .type_descriptor (types .VARCHAR (self .length ))
51
46
52
- def process_bind_param (self , value : t .Any , dialect : SQLiteDialect ) -> str :
47
+ def process_bind_param (self , value : t .Any , dialect : Dialect ) -> str :
53
48
return str (value )
54
49
55
- def process_result_value (self , value : t .Any , dialect : SQLiteDialect ) -> str :
50
+ def process_result_value (self , value : t .Any , dialect : Dialect ) -> str :
56
51
return str (value )
57
52
58
53
59
- Base : DeclarativeMeta = declarative_base ()
54
+ class Base (DeclarativeBase ):
55
+ pass
60
56
61
57
62
58
class Author (Base ):
63
59
__tablename__ = "authors"
64
- id : int = Column ( Integer , primary_key = True )
65
- name : str = Column (String (128 ), nullable = False , index = True )
60
+ id : Mapped [ int ] = mapped_column ( primary_key = True )
61
+ name : Mapped [ str ] = mapped_column (String (128 ), nullable = False , index = True )
66
62
67
63
def __repr__ (self ):
68
64
return "<Author(id='{id}', name='{name}')>" .format (id = self .id , name = self .name )
@@ -78,9 +74,9 @@ def __repr__(self):
78
74
79
75
class Image (Base ):
80
76
__tablename__ = "images"
81
- id : int = Column ( Integer , primary_key = True )
82
- path : str = Column (String (255 ), index = True )
83
- description : str = Column (String (255 ), nullable = True )
77
+ id : Mapped [ int ] = mapped_column ( primary_key = True )
78
+ path : Mapped [ str ] = mapped_column (String (255 ), index = True )
79
+ description : Mapped [ str ] = mapped_column (String (255 ), nullable = True )
84
80
85
81
def __repr__ (self ):
86
82
return "<Image(id='{id}', path='{path}')>" .format (id = self .id , path = self .path )
@@ -96,8 +92,8 @@ def __repr__(self):
96
92
97
93
class Tag (Base ):
98
94
__tablename__ = "tags"
99
- id : int = Column ( Integer , primary_key = True )
100
- name : str = Column (String (128 ), nullable = False , index = True )
95
+ id : Mapped [ int ] = mapped_column ( primary_key = True )
96
+ name : Mapped [ str ] = mapped_column (String (128 ), nullable = False , index = True )
101
97
102
98
def __repr__ (self ):
103
99
return "<Tag(id='{id}', name='{name}')>" .format (id = self .id , name = self .name )
@@ -115,27 +111,27 @@ class Misc(Base):
115
111
"""This model contains all possible MySQL types"""
116
112
117
113
__tablename__ = "misc"
118
- id : int = Column ( Integer , primary_key = True )
119
- big_integer_field : int = Column (BigInteger , default = 0 )
120
- blob_field : bytes = Column (BLOB , nullable = True , index = True )
121
- boolean_field : bool = Column ( Boolean , default = False )
122
- char_field : str = Column (CHAR (255 ), nullable = True )
123
- date_field : date = Column ( Date , nullable = True )
124
- date_time_field : datetime = Column ( DateTime , nullable = True )
125
- decimal_field : Decimal = Column (SQLiteNumeric (10 , 2 ), nullable = True )
126
- float_field : Decimal = Column (SQLiteNumeric (12 , 4 ), default = 0 )
127
- integer_field : int = Column ( Integer , default = 0 )
114
+ id : Mapped [ int ] = mapped_column ( primary_key = True )
115
+ big_integer_field : Mapped [ int ] = mapped_column (BigInteger , default = 0 )
116
+ blob_field : Mapped [ bytes ] = mapped_column (BLOB , nullable = True , index = True )
117
+ boolean_field : Mapped [ bool ] = mapped_column ( default = False )
118
+ char_field : Mapped [ str ] = mapped_column (CHAR (255 ), nullable = True )
119
+ date_field : Mapped [ date ] = mapped_column ( nullable = True )
120
+ date_time_field : Mapped [ datetime ] = mapped_column ( nullable = True )
121
+ decimal_field : Mapped [ Decimal ] = mapped_column (SQLiteNumeric (10 , 2 ), nullable = True )
122
+ float_field : Mapped [ Decimal ] = mapped_column (SQLiteNumeric (12 , 4 ), default = 0 )
123
+ integer_field : Mapped [ int ] = mapped_column ( default = 0 )
128
124
if environ .get ("LEGACY_DB" , "0" ) == "0" :
129
- json_field : t .Dict [str , t .Any ] = Column (JSON , nullable = True )
130
- numeric_field : Decimal = Column (SQLiteNumeric (12 , 4 ), default = 0 )
131
- real_field : float = Column (REAL (12 , 4 ), default = 0 )
132
- small_integer_field : int = Column (SmallInteger , default = 0 )
133
- string_field : str = Column (String (255 ), nullable = True )
134
- text_field : str = Column (Text , nullable = True )
135
- time_field : time = Column ( Time , nullable = True )
136
- varchar_field : str = Column (VARCHAR (255 ), nullable = True )
137
- timestamp_field : datetime = Column (TIMESTAMP , default = current_timestamp ())
138
- my_type_field : t .Any = Column (MyCustomType (255 ), nullable = True )
125
+ json_field : Mapped [ t .Dict [str , t .Any ]] = mapped_column (JSON , nullable = True )
126
+ numeric_field : Mapped [ Decimal ] = mapped_column (SQLiteNumeric (12 , 4 ), default = 0 )
127
+ real_field : Mapped [ float ] = mapped_column (REAL (12 , 4 ), default = 0 )
128
+ small_integer_field : Mapped [ int ] = mapped_column (SmallInteger , default = 0 )
129
+ string_field : Mapped [ str ] = mapped_column (String (255 ), nullable = True )
130
+ text_field : Mapped [ str ] = mapped_column (Text , nullable = True )
131
+ time_field : Mapped [ time ] = mapped_column ( nullable = True )
132
+ varchar_field : Mapped [ str ] = mapped_column (VARCHAR (255 ), nullable = True )
133
+ timestamp_field : Mapped [ datetime ] = mapped_column (TIMESTAMP , default = current_timestamp ())
134
+ my_type_field : Mapped [ t .Any ] = mapped_column (MyCustomType (255 ), nullable = True )
139
135
140
136
141
137
article_misc : Table = Table (
@@ -148,9 +144,9 @@ class Misc(Base):
148
144
149
145
class Media (Base ):
150
146
__tablename__ = "media"
151
- id : str = Column (CHAR (64 ), primary_key = True )
152
- title : str = Column (String (255 ), index = True )
153
- description : str = Column (String (255 ), nullable = True )
147
+ id : Mapped [ str ] = mapped_column (CHAR (64 ), primary_key = True )
148
+ title : Mapped [ str ] = mapped_column (String (255 ), index = True )
149
+ description : Mapped [ str ] = mapped_column (String (255 ), nullable = True )
154
150
155
151
def __repr__ (self ):
156
152
return "<Media(id='{id}', title='{title}')>" .format (id = self .id , title = self .title )
@@ -166,39 +162,39 @@ def __repr__(self):
166
162
167
163
class Article (Base ):
168
164
__tablename__ = "articles"
169
- id : int = Column ( Integer , primary_key = True )
170
- hash : str = Column (String (32 ), unique = True )
171
- slug : str = Column (String (255 ), index = True )
172
- title : str = Column (String (255 ), index = True )
173
- content : str = Column (Text , nullable = True , index = True )
174
- status : str = Column (CHAR (1 ), index = True )
175
- published : datetime = Column ( DateTime , nullable = True )
165
+ id : Mapped [ int ] = mapped_column ( primary_key = True )
166
+ hash : Mapped [ str ] = mapped_column (String (32 ), unique = True )
167
+ slug : Mapped [ str ] = mapped_column (String (255 ), index = True )
168
+ title : Mapped [ str ] = mapped_column (String (255 ), index = True )
169
+ content : Mapped [ str ] = mapped_column (Text , nullable = True , index = True )
170
+ status : Mapped [ str ] = mapped_column (CHAR (1 ), index = True )
171
+ published : Mapped [ datetime ] = mapped_column ( nullable = True )
176
172
# relationships
177
- authors : t .List [Author ] = relationship (
173
+ authors : Mapped [ t .List [Author ] ] = relationship (
178
174
"Author" ,
179
175
secondary = article_authors ,
180
176
backref = backref ("authors" , lazy = "dynamic" ),
181
177
lazy = "dynamic" ,
182
178
)
183
- tags : t .List [Tag ] = relationship (
179
+ tags : Mapped [ t .List [Tag ] ] = relationship (
184
180
"Tag" ,
185
181
secondary = article_tags ,
186
182
backref = backref ("tags" , lazy = "dynamic" ),
187
183
lazy = "dynamic" ,
188
184
)
189
- images : t .List [Image ] = relationship (
185
+ images : Mapped [ t .List [Image ] ] = relationship (
190
186
"Image" ,
191
187
secondary = article_images ,
192
188
backref = backref ("images" , lazy = "dynamic" ),
193
189
lazy = "dynamic" ,
194
190
)
195
- media : t .List [Media ] = relationship (
191
+ media : Mapped [ t .List [Media ] ] = relationship (
196
192
"Media" ,
197
193
secondary = article_media ,
198
194
backref = backref ("media" , lazy = "dynamic" ),
199
195
lazy = "dynamic" ,
200
196
)
201
- misc : t .List [Misc ] = relationship (
197
+ misc : Mapped [ t .List [Misc ] ] = relationship (
202
198
"Misc" ,
203
199
secondary = article_misc ,
204
200
backref = backref ("misc" , lazy = "dynamic" ),
0 commit comments