1
- from typing import Dict , List , Optional
1
+ from typing import List , Optional
2
2
3
- from table_meta .model import Column
3
+ from table_meta .model import Column , TableMeta
4
4
5
5
import omymodels .types as t
6
6
from omymodels .helpers import create_class_name , datetime_now_check
11
11
12
12
class ModelGenerator :
13
13
def __init__ (self ):
14
- self .imports = set ([ pt .base_model ])
14
+ self .imports = { pt .base_model }
15
15
self .types_for_import = ["Json" ]
16
16
self .datetime_import = False
17
17
self .typing_imports = set ()
18
18
self .custom_types = {}
19
19
self .uuid_import = False
20
20
self .prefix = ""
21
21
22
- def add_custom_type (self , target_type ) :
22
+ def add_custom_type (self , target_type : str ) -> Optional [ str ] :
23
23
column_type = self .custom_types .get (target_type , None )
24
24
_type = None
25
25
if isinstance (column_type , tuple ):
26
26
_type = column_type [1 ]
27
27
return _type
28
28
29
- def get_not_custom_type (self , column : Column ):
29
+ def get_not_custom_type (self , column : Column ) -> str :
30
30
_type = None
31
31
if "." in column .type :
32
32
_type = column .type .split ("." )[1 ]
33
33
else :
34
34
_type = column .type .lower ().split ("[" )[0 ]
35
- if _type == _type :
36
- _type = types_mapping .get (_type , _type )
35
+ _type = types_mapping .get (_type , _type )
37
36
if _type in self .types_for_import :
38
37
self .imports .add (_type )
39
38
elif "datetime" in _type :
@@ -45,40 +44,48 @@ def get_not_custom_type(self, column: Column):
45
44
self .uuid_import = True
46
45
return _type
47
46
48
- def generate_attr (self , column : Dict , defaults_off : bool ) -> str :
47
+ def generate_attr (self , column : Column , defaults_off : bool ) -> str :
49
48
_type = None
50
49
51
50
if column .nullable :
52
51
self .typing_imports .add ("Optional" )
53
52
column_str = pt .pydantic_optional_attr
54
53
else :
55
54
column_str = pt .pydantic_attr
55
+
56
56
if self .custom_types :
57
57
_type = self .add_custom_type (column .type )
58
58
if not _type :
59
59
_type = self .get_not_custom_type (column )
60
60
61
61
column_str = column_str .format (arg_name = column .name , type = _type )
62
62
63
- if column .default and defaults_off is False :
63
+ if column .default is not None and not defaults_off :
64
64
column_str = self .add_default_values (column_str , column )
65
65
66
66
return column_str
67
67
68
68
@staticmethod
69
- def add_default_values (column_str : str , column : Dict ) -> str :
69
+ def add_default_values (column_str : str , column : Column ) -> str :
70
+ # Handle datetime default values
70
71
if column .type .upper () in datetime_types :
71
72
if datetime_now_check (column .default .lower ()):
72
- # todo: need to add other popular PostgreSQL & MySQL functions
73
+ # Handle functions like CURRENT_TIMESTAMP
73
74
column .default = "datetime.datetime.now()"
74
- elif "'" not in column .default :
75
- column .default = f"'{ column ['default' ]} '"
75
+ elif column .default .upper () != "NULL" and "'" not in column .default :
76
+ column .default = f"'{ column .default } '"
77
+
78
+ # If the default is 'NULL', don't set a default in Pydantic (it already defaults to None)
79
+ if column .default .upper () == "NULL" :
80
+ return column_str
81
+
82
+ # Append the default value if it's not None (e.g., explicit default values like '0' or CURRENT_TIMESTAMP)
76
83
column_str += pt .pydantic_default_attr .format (default = column .default )
77
84
return column_str
78
85
79
86
def generate_model (
80
87
self ,
81
- table : Dict ,
88
+ table : TableMeta ,
82
89
singular : bool = True ,
83
90
exceptions : Optional [List ] = None ,
84
91
defaults_off : Optional [bool ] = False ,
0 commit comments