Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dev-dependencies = [
"pytest>=8.3.4",
"coverage>=7.6.9",
"docker>=7.1.0",
"sqlmodel>=0.0.27",
]

[build-system]
Expand Down
2 changes: 1 addition & 1 deletion src/sqlmodelgen/codegen/cir_to_full_ast/code_ir_to_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def gen_table_args(model_ir: ModelIR) -> ast.Assign | None:

return ast.Assign(
targets=[ast.Name('__table_args__')],
value=ast.List(
value=ast.Tuple(
elts=[table_arg.to_expr() for table_arg in model_ir.table_args]
)
)
Expand Down
6 changes: 5 additions & 1 deletion tests/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ def collect_table_name(stat: ast.Assign) -> str | None:
def collect_uniques(table_args: ast.expr) -> set[tuple[str]]:
uniques: set[tuple[str]] = set()

if isinstance(table_args, ast.List):
# TODO: this shall support the parsing of all the possible
# types of values __table_args__ could possess, I remember
# also a dictionary being possible and maybe something else
# other than a tuple
if isinstance(table_args, ast.Tuple):
for elt in table_args.elts:
if isinstance(elt, ast.Call) and isinstance(elt.func, ast.Name) and elt.func.id == 'UniqueConstraint':
uniques.add(tuple(arg.value for arg in elt.args if isinstance(arg, ast.Constant)))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gen_from_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_mysql():

class Hero(SQLModel, table=True):
__tablename__ = 'Hero'
__table_args__ = [UniqueConstraint('secret_name')]
__table_args__ = (UniqueConstraint('secret_name'), )
id: int | None
name: str | None
secret_name: str | None
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gen_from_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_gen_code():

class Users(SQLModel, table=True):
__tablename__ = 'users'
__table_args__ = [UniqueConstraint('email'), UniqueConstraint('name')]
__table_args__ = (UniqueConstraint('email'), UniqueConstraint('name'))
id: UUID = Field(primary_key=True, default_factory=uuid4)
email: str
name: str
Expand All @@ -101,7 +101,7 @@ class Participations(SQLModel, table=True):

class Leagues(SQLModel, table=True):
__tablename__ = 'leagues'
__table_args__ = [UniqueConstraint('name')]
__table_args__ = (UniqueConstraint('name'),)
id: UUID = Field(primary_key=True, default_factory=uuid4)
name: str
public: bool
Expand Down
8 changes: 6 additions & 2 deletions tests/test_gen_from_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,20 @@ def test_unique_single_column():
age INTEGER
);'''

assert collect_code_info(gen_code_from_sql(sql)) == collect_code_info('''from sqlmodel import SQLModel, Field, UniqueConstraint
code = gen_code_from_sql(sql)

assert collect_code_info(code) == collect_code_info('''from sqlmodel import SQLModel, Field, UniqueConstraint

class Hero(SQLModel, table = True):
\t__tablename__ = 'Hero'
\t__table_args__ = [UniqueConstraint('secret_name')]
\t__table_args__ = (UniqueConstraint('secret_name'), )

\tid: int = Field(primary_key=True)
\tname: str
\tsecret_name: str
\tage: int | None''')

exec(code, globals(), globals())


def test_datetime():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_collect_code_info():

class a_table(SQLModel, table = True):
__tablename__ = 'a_table'
__table_args__ = [UniqueConstraint('name')]
__table_args__ = (UniqueConstraint('name'), )
id: int | None = Field(primary_key=True)
name: str
email: str | None''')
Expand Down
Loading