From df7a96afbe9f72787be22a378eb1013ab82f9d6f Mon Sep 17 00:00:00 2001 From: touale <1367642349@qq.com> Date: Tue, 3 Dec 2024 15:51:28 +0800 Subject: [PATCH 1/4] Correct imports and improve generation logic in `SQLModelGenerator` --- src/sqlacodegen/generators.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/sqlacodegen/generators.py b/src/sqlacodegen/generators.py index 21eadb63..6edcb530 100644 --- a/src/sqlacodegen/generators.py +++ b/src/sqlacodegen/generators.py @@ -292,8 +292,21 @@ def group_imports(self) -> list[list[str]]: stdlib_imports: list[str] = [] thirdparty_imports: list[str] = [] + has_imports: set[str] = set() for package in sorted(self.imports): - imports = ", ".join(sorted(self.imports[package])) + if duplicate_import := self.imports[package] & has_imports: + print( + f"WARN: Duplicate imports `{duplicate_import}` are detected " + f"from the package `{package}` and will be filtered, " + f"which may cause abnormal behavior." + ) + + current_import = sorted( + name for name in self.imports[package] if name not in duplicate_import + ) + has_imports = has_imports | set(current_import) + + imports = ", ".join(current_import) collection = thirdparty_imports if package == "__future__": collection = future_imports @@ -448,7 +461,11 @@ def render_column( kwargs["key"] = column.key if is_primary: kwargs["primary_key"] = True - if not column.nullable and not is_sole_pk and is_table: + if ( + not column.nullable + and not is_sole_pk + and (is_table or isinstance(self, SQLModelGenerator)) + ): kwargs["nullable"] = False if is_unique: @@ -482,10 +499,11 @@ def render_column( if comment: kwargs["comment"] = repr(comment) - if is_table: + if is_table or isinstance(self, SQLModelGenerator): self.add_import(Column) return render_callable("Column", *args, kwargs=kwargs) else: + self.add_literal_import("sqlalchemy.orm", "mapped_column") return render_callable("mapped_column", *args, kwargs=kwargs) def render_column_type(self, coltype: object) -> str: From 2e8ff91597d27b411873db267454e52ffe46678c Mon Sep 17 00:00:00 2001 From: touale <1367642349@qq.com> Date: Tue, 3 Dec 2024 16:22:23 +0800 Subject: [PATCH 2/4] Complete the CI workflow to install basic dependencies --- .github/workflows/test.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c62b7a8e..7b230cc8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,7 +21,9 @@ jobs: allow-prereleases: true cache: pip cache-dependency-path: pyproject.toml - - name: Install dependencies + - name: Install basic dependencies + run: pip install -e . + - name: Install test dependencies run: pip install -e .[test] - name: Test with pytest run: coverage run -m pytest From 79582d886ebef0ad63b48350f31af4870c368ef8 Mon Sep 17 00:00:00 2001 From: touale <1367642349@qq.com> Date: Tue, 3 Dec 2024 16:27:11 +0800 Subject: [PATCH 3/4] Revert "Complete the CI workflow to install basic dependencies" This reverts commit 2e8ff91597d27b411873db267454e52ffe46678c. --- .github/workflows/test.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7b230cc8..c62b7a8e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,9 +21,7 @@ jobs: allow-prereleases: true cache: pip cache-dependency-path: pyproject.toml - - name: Install basic dependencies - run: pip install -e . - - name: Install test dependencies + - name: Install dependencies run: pip install -e .[test] - name: Test with pytest run: coverage run -m pytest From dc4ded5a20e8611c4d9720159ef8b0973a939460 Mon Sep 17 00:00:00 2001 From: touale <1367642349@qq.com> Date: Tue, 3 Dec 2024 16:27:55 +0800 Subject: [PATCH 4/4] Add sqlmodel dependency install step in CI test workflow --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c62b7a8e..e4f6eca3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,6 +23,8 @@ jobs: cache-dependency-path: pyproject.toml - name: Install dependencies run: pip install -e .[test] + - name: Install sqlmodel dependency + run: pip install -e .[sqlmodel] - name: Test with pytest run: coverage run -m pytest - name: Upload Coverage