Correct imports and improve generation logic in SQLModelGenerator
#352
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In fact, I found that there are some problems in the code generated by
sqlacodegen
forsqlmodel
, which is manifested in the introduction of redundant classCHAR
and themapped_column
does not been import. The following is a detailed description of the problem:(1) When encountering
CHAR(36)
andCHAR(36, 'utf8mb4_general_ci')
,sqlacodegen
will repeatedly import from different sourcesThe adjustment I made was to focus on sorted and high-priority imports, and to make a warn about duplicate imports.
for example:
In fact, "from sqlalchemy import CHAR" supports general projects of multiple databases, while "from sqlalchemy.dialects.mysql import CHAR" is specifically for MySQL projects. If you project requires cross-database compatibility or you are not sure about the target database type, it is safer to use "sqlalchemy.CHAR".
(2)
sqlmodel
does not supportmapped_column
in the latest release(https://github.com/fastapi/sqlmodel/releases/tag/0.0.22), although someone has proposed a PR (fastapi/sqlmodel#1143), soColumn
should be used for the generation of sqlmodels for the time being