You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 sqlacodegen library to generate SQLAlchemy models from a database schema that includes a CHAR column with specific collation settings.
Attempt to use the generated SQLAlchemy model in an application.
Observe the TypeError with 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.
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
CREATETABLE `TableNameForIssue` (
`id`intNOT 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'
The text was updated successfully, but these errors were encountered:
Yes, I just tested it with version 3.0.0rc3 and it's the same
Note: Part of SQLAlchemy
# dialects\mysql\types.py
...
classCHAR(_StringType, sqltypes.CHAR):
"""MySQL CHAR type, for fixed-length character data."""__visit_name__="CHAR"def__init__(self, length=None, **kwargs):
"""Construct a CHAR. :param length: Maximum data length, in characters. :param binary: Optional, use the default binary collation for the national character set. This does not affect the type of data stored, use a BINARY type for binary data. :param collation: Optional, request a particular collation. Must be compatible with the national character set. """super(CHAR, self).__init__(length=length, **kwargs)
...
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 aCHAR
column contains an error that causes aTypeError
during application execution.Steps to Reproduce
sqlacodegen
library to generate SQLAlchemy models from a database schema that includes aCHAR
column with specific collation settings.TypeError
with the following traceback:Expected Behavior
I expected the generated SQLAlchemy model to correctly represent the
CHAR
column with the specified collation settings and not produce aTypeError
during application execution.Actual Behavior
The generated code for the
CHAR
column includes incorrect syntax, resulting in aTypeError
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
Additional Information
I believe the issue may be related to how
sqlacodegen
handles collation settings forCHAR
columns. The generated code attempts to pass three arguments to theColumn
constructor instead of the expected two, which leads to theTypeError
. Manually adjusting the generated code to specify the collation settings separately resolves the issue.Database schema for reproducing the bug
Environment
The text was updated successfully, but these errors were encountered: