Skip to content

Commit 9817c43

Browse files
authored
fix sqlmodels generation (xnuinside#60)
* fix sqlmodels generation * fix flake8 issues
1 parent 4893e38 commit 9817c43

16 files changed

Lines changed: 115 additions & 70 deletions

File tree

CHANGELOG.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
**v0.16.0**
2+
3+
### Updates
4+
1. fix character varying type - https://github.com/xnuinside/omymodels/issues/59
5+
2. sqlalchemy import removed from generation in sqlmodels if it is not used
6+
3. = Field() - is not placed in SQLModel if there is no defaults or other settings to the field
7+
8+
19
**v0.15.1**
210
## Updates
311
1. Foreign Key processing updates - https://github.com/xnuinside/omymodels/pull/55

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,14 @@ If you see any bugs or have any suggestions - feel free to open the issue. Any h
307307
One more time, big 'thank you!' goes to https://github.com/archongum for Web-version: https://archon-omymodels-online.hf.space/
308308

309309
## Changelog
310+
**v0.16.0**
311+
312+
### Updates
313+
1. fix character varying type - https://github.com/xnuinside/omymodels/issues/59
314+
2. sqlalchemy import removed from generation in sqlmodels if it is not used
315+
3. = Field() - is not placed in SQLModel if there is no defaults or other settings to the field
316+
317+
310318
**v0.15.1**
311319
## Updates
312320
1. Foreign Key processing updates - https://github.com/xnuinside/omymodels/pull/55

docs/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,16 @@ One more time, big 'thank you!' goes to https://github.com/archongum for Web-ver
330330
Changelog
331331
---------
332332

333+
**v0.16.0**
334+
335+
Updates
336+
^^^^^^^
337+
338+
339+
#. fix character varying type - https://github.com/xnuinside/omymodels/issues/59
340+
#. sqlalchemy import removed from generation in sqlmodels if it is not used
341+
#. = Field() - is not placed in SQLModel if there is no defaults or other settings to the field
342+
333343
**v0.15.1**
334344

335345
Updates

omymodels/cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def version(**kwargs):
1313

1414
def cli():
1515
omm_cli = argparse.ArgumentParser(
16-
description="O! My Models. Create GinoORM models from SQL DDL"
16+
description="O! My Models. "
17+
"Create SQLModels, SQLAlchemy, GinoORM and other models from SQL DDL or another models"
1718
)
1819

1920
omm_cli.add_argument(

omymodels/from_ddl.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ def generate_models_file(
153153
schema_global=schema_global,
154154
defaults_off=defaults_off,
155155
)
156-
header += generator.create_header(data["tables"], schema=schema_global)
156+
header += generator.create_header(
157+
data["tables"], schema=schema_global, models_str=models_str
158+
)
157159
else:
158160
models_type = "enum"
159161
output = render_jinja2_template(models_type, models_str, header)

omymodels/models/enum/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def create_types(self) -> str:
7474
self.custom_types = {x.name: ("db.Enum", x.name) for x in self.types}
7575
return types_str
7676

77-
def create_header(self) -> str:
77+
def create_header(self, *args, **kwargs) -> str:
7878
self.enum_imports = list(self.enum_imports)
7979
self.enum_imports.sort()
8080
return enum_import.format(enums=", ".join(self.enum_imports)) + "\n"

omymodels/models/gino/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ def generate_model(
5959
# create sequence
6060
return model
6161

62-
def create_header(self, tables: List[Dict], schema: bool = False) -> str:
62+
def create_header(
63+
self, tables: List[Dict], schema: bool = False, *args, **kwargs
64+
) -> str:
6365
"""header of the file - imports & gino init"""
6466
header = ""
6567
if "func" in self.state:

omymodels/models/sqlalchemy/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ def generate_model(
6464
model = logic.add_table_args(self, model, table, schema_global)
6565
return model
6666

67-
def create_header(self, tables: List[Dict], schema: bool = False) -> str:
67+
def create_header(
68+
self, tables: List[Dict], schema: bool = False, *args, **kwargs
69+
) -> str:
6870
"""header of the file - imports & sqlalchemy init"""
6971
header = ""
7072
if "func" in self.state:

omymodels/models/sqlalchemy_core/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ def generate_model(self, data: Dict, *args, **kwargs) -> str:
234234
model += index
235235
return model
236236

237-
def create_header(self, tables: List[Dict], schema: bool = False) -> str:
237+
def create_header(
238+
self, tables: List[Dict], schema: bool = False, *args, **kwargs
239+
) -> str:
238240
"""header of the file - imports & sqlalchemy init"""
239241
header = ""
240242
if "func" in self.state:

omymodels/models/sqlmodel/core.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ def generate_model(
119119
**kwargs,
120120
) -> str:
121121
"""method to prepare one Model defention - name & tablename & columns"""
122-
model = ""
123122

124123
model = st.model_template.format(
125124
model_name=create_class_name(table.name, singular, exceptions),
@@ -133,26 +132,31 @@ def generate_model(
133132
col_str = st.column_template.format(
134133
column_name=column.name.replace(" ", "_"), column_type=pydantic_type_str
135134
)
136-
137-
col_str = logic.setup_column_attributes(
138-
column, table.primary_key, col_str, table, schema_global, st, self
135+
attrs_col_str = logic.setup_column_attributes(
136+
column, table.primary_key, "", table, schema_global, st, self
139137
)
140138
if column_type["sa"]:
141139
sa_type = types.add_size_to_orm_column(column_type["sa"], column)
142-
col_str += st.sa_type.format(satype=sa_type)
143-
col_str += ")\n"
144-
145-
col_str = col_str.replace("(, ", "(")
146-
140+
attrs_col_str += st.sa_type.format(satype=sa_type)
141+
if attrs_col_str:
142+
attrs_col_str = attrs_col_str.replace(",", "", 1).strip()
143+
col_str += st.field_template.format(attr_data=attrs_col_str)
144+
col_str += "\n"
147145
model += col_str
148146
if table.indexes or table.alter or table.checks or not schema_global:
149147
model = self.add_table_args(model, table, schema_global)
150148
return model
151149

152-
def create_header(self, tables: List[Dict], schema: bool = False) -> str:
150+
def create_header(
151+
self,
152+
tables: List[Dict],
153+
models_str: str,
154+
schema: bool = False,
155+
) -> str:
153156
"""header of the file - imports & sqlalchemy init"""
154157
header = ""
155-
header += st.sqlalchemy_import # Do we always need this import?
158+
if "sa." in models_str:
159+
header += st.sqlalchemy_import # Do we always need this import?
156160
if "func" in self.state:
157161
header += st.sql_alchemy_func_import + "\n"
158162
if self.postgresql_dialect_cols:

0 commit comments

Comments
 (0)