-
Notifications
You must be signed in to change notification settings - Fork 279
Description
Things to check first
-
I have searched the existing issues and didn't find my bug already reported there
-
I have checked that my bug is still present in the latest release
Sqlacodegen version
2.3.0.post1
SQLAlchemy version
2.0.15
RDBMS vendor
MySQL (or compatible)
What happened?
Summary
I encountered an issue when using the sqlacodegen library to generate SQLAlchemy models from my database schema. The generated code for a CHAR column contains an error that causes a TypeError during application execution.
Steps to Reproduce
- Use the
sqlacodegenlibrary to generate SQLAlchemy models from a database schema that includes aCHARcolumn with specific collation settings. - Attempt to use the generated SQLAlchemy model in an application.
- Observe the
TypeErrorwith the following traceback:
[ERROR] TypeError: init() takes from 1 to 2 positional arguments but 3 were given
Traceback (most recent call last):
...
...
File "models.py", line XX, in <module>
result_code = Column(CHAR(1, "utf8mb3_bin"), nullable=False)Expected Behavior
I expected the generated SQLAlchemy model to correctly represent the CHAR column with the specified collation settings and not produce a TypeError during application execution.
Actual Behavior
The generated code for the CHAR column includes incorrect syntax, resulting in a TypeError when the model is used in the application.
How I solved
Instead of entering the arguments in order in the CHAR method, the collation keyword was defined and used.
result_code = Column(CHAR(1, collation="utf8mb3_bin"), nullable=False)Environment
- SQLAlchemy Version: 2.0.15
- sqlacodegen Version: 2.3.0.post1
Additional Information
I believe the issue may be related to how sqlacodegen handles collation settings for CHAR columns. The generated code attempts to pass three arguments to the Column constructor instead of the expected two, which leads to the TypeError. Manually adjusting the generated code to specify the collation settings separately resolves the issue.
Database schema for reproducing the bug
Environment
- MySQL Version: 8.0.34
- Infra: AWS RDS
CREATE TABLE `TableNameForIssue` (
`id` int NOT NULL AUTO_INCREMENT,
`plate_number` varchar(100) COLLATE utf8mb3_bin NOT NULL,
`result_code` char(1) COLLATE utf8mb3_bin NOT NULL,
`result_msg` varchar(100) COLLATE utf8mb3_bin DEFAULT NULL,
`createdAt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updatedAt` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `TableNameForIssue_createdAt_IDX` (`createdAt`) USING BTREE,
KEY `TableNameForIssue_plate_number_IDX` (`plate_number`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin COMMENT='comment'