Skip to content

Commit 3f7b067

Browse files
authored
Merge pull request xnuinside#63 from rayliverified/main
Fix Incorrect Column Type Crash
2 parents d95c69c + b5d33bd commit 3f7b067

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

omymodels/models/dataclass/core.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from typing import Dict, List, Optional
1+
from typing import List, Optional
2+
3+
from table_meta import TableMeta
4+
from table_meta.model import Column
25

36
import omymodels.types as t
47
from omymodels.helpers import create_class_name, datetime_now_check
@@ -24,7 +27,7 @@ def add_custom_type(self, _type: str) -> str:
2427
column_type = column_type[0]
2528
return _type
2629

27-
def generate_attr(self, column: Dict, defaults_off: bool) -> str:
30+
def generate_attr(self, column: Column, defaults_off: bool) -> str:
2831
column_str = dt.dataclass_attr
2932

3033
if "." in column.type:
@@ -57,19 +60,19 @@ def generate_attr(self, column: Dict, defaults_off: bool) -> str:
5760
return column_str
5861

5962
@staticmethod
60-
def add_column_default(column_str: str, column: Dict) -> str:
63+
def add_column_default(column_str: str, column: Column) -> str:
6164
if column.type.upper() in datetime_types:
6265
if datetime_now_check(column.default.lower()):
6366
# todo: need to add other popular PostgreSQL & MySQL functions
6467
column.default = dt.field_datetime_now
6568
elif "'" not in column.default:
66-
column.default = f"'{column['default']}'"
69+
column.default = f"'{column.default}'"
6770
column_str += dt.dataclass_default_attr.format(default=column.default)
6871
return column_str
6972

7073
def generate_model(
7174
self,
72-
table: Dict,
75+
table: TableMeta,
7376
singular: bool = True,
7477
exceptions: Optional[List] = None,
7578
defaults_off: Optional[bool] = False,

omymodels/models/pydantic/core.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from typing import Dict, List, Optional
1+
from typing import List, Optional
22

3-
from table_meta.model import Column
3+
from table_meta.model import Column, TableMeta
44

55
import omymodels.types as t
66
from omymodels.helpers import create_class_name, datetime_now_check
@@ -11,29 +11,28 @@
1111

1212
class ModelGenerator:
1313
def __init__(self):
14-
self.imports = set([pt.base_model])
14+
self.imports = {pt.base_model}
1515
self.types_for_import = ["Json"]
1616
self.datetime_import = False
1717
self.typing_imports = set()
1818
self.custom_types = {}
1919
self.uuid_import = False
2020
self.prefix = ""
2121

22-
def add_custom_type(self, target_type):
22+
def add_custom_type(self, target_type: str) -> Optional[str]:
2323
column_type = self.custom_types.get(target_type, None)
2424
_type = None
2525
if isinstance(column_type, tuple):
2626
_type = column_type[1]
2727
return _type
2828

29-
def get_not_custom_type(self, column: Column):
29+
def get_not_custom_type(self, column: Column) -> str:
3030
_type = None
3131
if "." in column.type:
3232
_type = column.type.split(".")[1]
3333
else:
3434
_type = column.type.lower().split("[")[0]
35-
if _type == _type:
36-
_type = types_mapping.get(_type, _type)
35+
_type = types_mapping.get(_type, _type)
3736
if _type in self.types_for_import:
3837
self.imports.add(_type)
3938
elif "datetime" in _type:
@@ -45,40 +44,48 @@ def get_not_custom_type(self, column: Column):
4544
self.uuid_import = True
4645
return _type
4746

48-
def generate_attr(self, column: Dict, defaults_off: bool) -> str:
47+
def generate_attr(self, column: Column, defaults_off: bool) -> str:
4948
_type = None
5049

5150
if column.nullable:
5251
self.typing_imports.add("Optional")
5352
column_str = pt.pydantic_optional_attr
5453
else:
5554
column_str = pt.pydantic_attr
55+
5656
if self.custom_types:
5757
_type = self.add_custom_type(column.type)
5858
if not _type:
5959
_type = self.get_not_custom_type(column)
6060

6161
column_str = column_str.format(arg_name=column.name, type=_type)
6262

63-
if column.default and defaults_off is False:
63+
if column.default is not None and not defaults_off:
6464
column_str = self.add_default_values(column_str, column)
6565

6666
return column_str
6767

6868
@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
7071
if column.type.upper() in datetime_types:
7172
if datetime_now_check(column.default.lower()):
72-
# todo: need to add other popular PostgreSQL & MySQL functions
73+
# Handle functions like CURRENT_TIMESTAMP
7374
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)
7683
column_str += pt.pydantic_default_attr.format(default=column.default)
7784
return column_str
7885

7986
def generate_model(
8087
self,
81-
table: Dict,
88+
table: TableMeta,
8289
singular: bool = True,
8390
exceptions: Optional[List] = None,
8491
defaults_off: Optional[bool] = False,

omymodels/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def process_types_after_models_parser(column_data: Column) -> Column:
116116
return column_data
117117

118118

119-
def prepare_column_data(column_data: Column) -> str:
119+
def prepare_column_data(column_data: Column) -> Column:
120120
if "." in column_data.type or "(":
121121
column_data = process_types_after_models_parser(column_data)
122122
return column_data

0 commit comments

Comments
 (0)