-
Notifications
You must be signed in to change notification settings - Fork 256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error in Generated SQLAlchemy Model for CHAR Column #285
Comments
Have you tried with the latest 3.0 release candidate? |
Yes, I just tested it with version 3.0.0rc3 and it's the same Note: Part of SQLAlchemy# dialects\mysql\types.py
...
class CHAR(_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)
... |
@agronholm this looks important. Maybe not for this milestone, but for the one after that |
I can take a stab at this tomorrow if I have time. |
I can also have a look. Just wanted to make sure it doesn't get overlooked. I marked it for milestone 3.1. |
@agronholm looking at this issue I can report that:
The only remaining issue here, is the If I were to add some parent type signature parsing, it'd probably solve this issue .wdyt? |
Finding the correct kwargs to add is a massive PITA. Does one of its parent classes take such keyword arguments? |
To answer my own question, yes. But it takes plenty of other keyword arguments too. It seems just like I feared - that there is no way to automatically determine the correct keyword arguments to render. |
In this case - the type hierarchy is |
What passed kwargs dict? Where do we get that? |
The signature and parameters extracted from a |
I'm asking how we can reliably get the values passed to the class initializer. The classes are in no way obligated to retain attributes with the same names. |
We're already getting the reflected type properly from SQLAlchemy. Meaning, the values it holds already rely on how implementation classes declare constructor field names and pass them up the type hierarchy. All we need to do is follow hierarchy constructor field trail in a best effort manner in order to make sure types are being rendered following the same principal. I believe it should work |
OK so in other words, follow the MRO, get initializer signatures, extract argument names and their default values, check for attributes with matching names whose values differ from the defaults? |
Pretty much so |
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: