fix generated boolean default value in mssql#10
Conversation
hunyadi
left a comment
There was a problem hiding this comment.
Thanks for identifying this bug with Boolean default values in Microsoft SQL Server! I did not spot any deal-breaker issues, I am happy to merge tomorrow. Nonetheless, I have proposed some suggestions to make the code easier to read, and better convey the developer's intent.
hunyadi
left a comment
There was a problem hiding this comment.
Thanks for the updates! Drop me a message if you need a new version released to PyPI with the changes included.
| if value is True: | ||
| return "1" | ||
|
|
||
| if value is False: | ||
| return "0" | ||
|
|
||
| raise NotImplementedError( | ||
| f"Cannot convert boolean value to SQL literal: {value}" | ||
| ) |
There was a problem hiding this comment.
A more elegant approach to this problem is to test for valid type first:
if not isinstance(value, bool):
raise TypeError(f"expected: value of type `bool`, got: {type(value)}")
This allows type checkers to infer that value is either True or False in subsequent code (the only allowed values for type bool).
|
Thank you very much Levente, I'm still testing some use cases, I'll let you know when you can go ahead with the pypi release. |
|
@hunyadi, I found an issue still in MSSQL; the DEFAULT-constraint-name is not table-specific which prevents the creation of multiple tables with the same column-name and those columns having default values. I'm looking for a possible solution. Please don't release a new PyPI version until this gets resolved. |
Hi Levente,
I have created a possible fix for a bug or not-yet-implemented feature.
Issue: When a Python class contains a boolean field with a default value, e.g.
boolean_false: bool = Falseornullable_boolean_true: Optional[bool] = Truethen theCREATE TABLEstatement generated bypysqlsyncfor MSSQL contains an invalid syntax:"boolean_false" bit NOT NULL CONSTRAINT "df_boolean_false" DEFAULT FALSE-- invalid in MSSQL"nullable_boolean_true" bit CONSTRAINT "df_nullable_boolean_true" DEFAULT TRUE-- invalid in MSSQL"boolean_false" bit NOT NULL CONSTRAINT "df_boolean_false" DEFAULT 0-- valid in MSSQL"nullable_boolean_true" bit CONSTRAINT "df_nullable_boolean_true" DEFAULT 1-- valid in MSSQLOther dialects (PostgreSQL etc) are fine.
The implementation I created is my best effort understanding and extending the code base, please advise how it could be made even better.
Thanks, Robert