@@ -6,15 +6,13 @@ def test_with_enums():
6
6
import decimal
7
7
from typing import Optional
8
8
from sqlmodel import Field, SQLModel
9
-
10
9
from enum import Enum
11
10
import sqlalchemy as sa
12
11
from sqlalchemy.sql import func
13
12
from sqlalchemy.dialects.postgresql import JSON
14
13
from pydantic import Json, UUID4
15
14
16
15
17
-
18
16
class MaterialType(str, Enum):
19
17
20
18
article = 'article'
@@ -26,13 +24,13 @@ class Material(SQLModel, table=True):
26
24
__tablename__ = 'material'
27
25
28
26
id: Optional[int] = Field(default=None, primary_key=True)
29
- title: str = Field()
27
+ title: str
30
28
description: Optional[str] = Field(sa_type=sa.Text())
31
- link: str = Field()
29
+ link: str
32
30
type: Optional[MaterialType] = Field(sa_type=sa.Enum(MaterialType))
33
31
additional_properties: Optional[Json] = Field(sa_column_kwargs={'server_default': '{"key": "value"}'}, sa_type=JSON())
34
32
created_at: Optional[datetime.datetime] = Field(sa_column_kwargs={'server_default': func.now()})
35
- updated_at: Optional[datetime.datetime] = Field()
33
+ updated_at: Optional[datetime.datetime]
36
34
""" # noqa: E501
37
35
ddl = """
38
36
CREATE TYPE "material_type" AS ENUM (
@@ -61,20 +59,17 @@ def test_foreign_keys():
61
59
from typing import Optional
62
60
from sqlmodel import Field, SQLModel
63
61
64
- import sqlalchemy as sa
65
-
66
-
67
62
68
63
class Materials(SQLModel, table=True):
69
64
70
65
__tablename__ = 'materials'
71
66
72
67
id: Optional[int] = Field(default=None, primary_key=True)
73
- title: str = Field()
74
- description: Optional[str] = Field()
75
- link: Optional[str] = Field()
76
- created_at: Optional[datetime.datetime] = Field()
77
- updated_at: Optional[datetime.datetime] = Field()
68
+ title: str
69
+ description: Optional[str]
70
+ link: Optional[str]
71
+ created_at: Optional[datetime.datetime]
72
+ updated_at: Optional[datetime.datetime]
78
73
79
74
80
75
class MaterialAttachments(SQLModel, table=True):
@@ -90,10 +85,10 @@ class Attachments(SQLModel, table=True):
90
85
__tablename__ = 'attachments'
91
86
92
87
id: Optional[int] = Field(default=None, primary_key=True)
93
- title: Optional[str] = Field()
94
- description: Optional[str] = Field()
95
- created_at: Optional[datetime.datetime] = Field()
96
- updated_at: Optional[datetime.datetime] = Field()
88
+ title: Optional[str]
89
+ description: Optional[str]
90
+ created_at: Optional[datetime.datetime]
91
+ updated_at: Optional[datetime.datetime]
97
92
"""
98
93
ddl = """
99
94
@@ -139,20 +134,17 @@ def test_foreign_keys_defined_inline():
139
134
from typing import Optional
140
135
from sqlmodel import Field, SQLModel
141
136
142
- import sqlalchemy as sa
143
-
144
-
145
137
146
138
class Materials(SQLModel, table=True):
147
139
148
140
__tablename__ = 'materials'
149
141
150
142
id: Optional[int] = Field(default=None, primary_key=True)
151
- title: str = Field()
152
- description: Optional[str] = Field()
153
- link: Optional[str] = Field()
154
- created_at: Optional[datetime.datetime] = Field()
155
- updated_at: Optional[datetime.datetime] = Field()
143
+ title: str
144
+ description: Optional[str]
145
+ link: Optional[str]
146
+ created_at: Optional[datetime.datetime]
147
+ updated_at: Optional[datetime.datetime]
156
148
157
149
158
150
class MaterialAttachments(SQLModel, table=True):
@@ -168,10 +160,10 @@ class Attachments(SQLModel, table=True):
168
160
__tablename__ = 'attachments'
169
161
170
162
id: Optional[int] = Field(default=None, primary_key=True)
171
- title: Optional[str] = Field()
172
- description: Optional[str] = Field()
173
- created_at: Optional[datetime.datetime] = Field()
174
- updated_at: Optional[datetime.datetime] = Field()
163
+ title: Optional[str]
164
+ description: Optional[str]
165
+ created_at: Optional[datetime.datetime]
166
+ updated_at: Optional[datetime.datetime]
175
167
"""
176
168
ddl = """
177
169
@@ -212,20 +204,17 @@ def test_multi_col_pk_and_fk():
212
204
import decimal
213
205
from typing import Optional
214
206
from sqlmodel import Field, SQLModel
215
-
216
- import sqlalchemy as sa
217
207
from sqlalchemy.sql import func
218
208
219
209
220
-
221
210
class Complexpk(SQLModel, table=True):
222
211
223
212
__tablename__ = 'complexpk'
224
213
225
214
complex_id: Optional[int] = Field(default=None, primary_key=True)
226
215
date_part: Optional[datetime.datetime] = Field(sa_column_kwargs={'server_default': func.now()}, default=None, primary_key=True)
227
- title: str = Field()
228
- description: Optional[str] = Field()
216
+ title: str
217
+ description: Optional[str]
229
218
230
219
231
220
class LinkedTo(SQLModel, table=True):
@@ -235,7 +224,7 @@ class LinkedTo(SQLModel, table=True):
235
224
id: Optional[int] = Field(default=None, primary_key=True)
236
225
complexpk_complex_id: Optional[int] = Field(foreign_key='complexpk.complex_id')
237
226
complexpk_date_part: Optional[int] = Field(foreign_key='complexpk.date_part')
238
- comment: Optional[str] = Field()
227
+ comment: Optional[str]
239
228
""" # noqa: E501
240
229
241
230
ddl = """
@@ -267,15 +256,13 @@ def test_upper_name_produces_the_same_result():
267
256
import decimal
268
257
from typing import Optional
269
258
from sqlmodel import Field, SQLModel
270
-
271
259
from enum import Enum
272
260
import sqlalchemy as sa
273
261
from sqlalchemy.sql import func
274
262
from sqlalchemy.dialects.postgresql import JSON
275
263
from pydantic import Json, UUID4
276
264
277
265
278
-
279
266
class MaterialType(str, Enum):
280
267
281
268
article = 'article'
@@ -287,13 +274,13 @@ class Material(SQLModel, table=True):
287
274
__tablename__ = 'material'
288
275
289
276
id: Optional[int] = Field(default=None, primary_key=True)
290
- title: str = Field()
277
+ title: str
291
278
description: Optional[str] = Field(sa_type=sa.Text())
292
- link: str = Field()
279
+ link: str
293
280
type: Optional[MaterialType] = Field(sa_type=sa.Enum(MaterialType))
294
281
additional_properties: Optional[Json] = Field(sa_column_kwargs={'server_default': '{"key": "value"}'}, sa_type=JSON())
295
282
created_at: Optional[datetime.datetime] = Field(sa_column_kwargs={'server_default': func.now()})
296
- updated_at: Optional[datetime.datetime] = Field()
283
+ updated_at: Optional[datetime.datetime]
297
284
""" # noqa: E501
298
285
ddl = """
299
286
CREATE TYPE "material_type" AS ENUM (
@@ -322,9 +309,6 @@ def test_foreign_keys_in_different_schema():
322
309
from typing import Optional
323
310
from sqlmodel import Field, SQLModel
324
311
325
- import sqlalchemy as sa
326
-
327
-
328
312
329
313
class Table1(SQLModel, table=True):
330
314
@@ -371,3 +355,27 @@ class Table2(SQLModel, table=True):
371
355
"""
372
356
result = create_models (ddl , schema_global = False , models_type = "sqlmodel" )["code" ]
373
357
assert result == expected
358
+
359
+
360
+ def test_sqlmodel_varying ():
361
+ ddl = """
362
+ CREATE TABLE qwe (
363
+ id integer NOT NULL,
364
+ name character varying(255),
365
+ );
366
+ """
367
+ result = create_models (ddl , models_type = "sqlmodel" )["code" ]
368
+ expected = """import datetime
369
+ import decimal
370
+ from typing import Optional
371
+ from sqlmodel import Field, SQLModel
372
+
373
+
374
+ class Qwe(SQLModel, table=True):
375
+
376
+ __tablename__ = 'qwe'
377
+
378
+ id: int
379
+ name: Optional[str]
380
+ """
381
+ assert expected == result
0 commit comments