Add support for SQL Variant fields#356
Conversation
sql_variant fields in databases causes the following error "ODBC SQL type -150 is not yet supported. column-index=0 type=-150', 'HY106'" This commit converts these fields into a string and returns
|
Hi @akilude, thank you for contributing. Could you provide a test case for this feature? |
Hi, Sure, will add it early next week. |
|
@dauinsight, could you please give me a couple of pointers on how to provide a test case for this. This feature will mainly be used with existing external databases. When we have an external mssql database with a field of type "sql_variant", this PR will convert the data to string and return it to django. I think there are only test cases for database created with django? |
Good question, we can use Django's RunSQL function to make a custom migration with a sql_variant type: https://docs.djangoproject.com/en/5.0/ref/migration-operations/#runsql |
|
Hi @akilude, an example test might look like: |
|
Thanks @dauinsight, bit busy with work, will add it in a day or two. |
|
@dauinsight, apologies for the delay, have added the test case. Please let me know if any changes are needed. Thanks. |
|
|
||
| def handle_sql_variant_as_string(value): | ||
| # SQL variant of type 150 is not supported, convert it to string and return | ||
| return value.decode('utf-16le') |
There was a problem hiding this comment.
I'm not too familiar with the sql_variant type but it looks like this method of decoding is likely to result in incorrect data.
Since sql_variant can contain multiple types, unpacking the data in a reliable manner seems difficult... We might be better off doing an explicit CAST in sql.
There was a problem hiding this comment.
@dauinsight We can find the base type of the field https://learn.microsoft.com/en-us/sql/t-sql/functions/sql-variant-property-transact-sql?view=sql-server-ver16
in the converter function, shall we convert whatever type to string and return.
There was a problem hiding this comment.
@dauinsight, please let me know the best way to proceed. Thanks.
Adds support for sql_variant field which causes the following error:
"ODBC SQL type -150 is not yet supported. column-index=0 type=-150', 'HY106'"
This commit converts these fields output to a string