Open niekheinen opened 10 months ago
Hi @niekheinen, the server behavior is actually by design as we're expecting the same type for the field.
For more details and workaround: https://github.com/mkleehammer/pyodbc/issues/419
Hi @dauinsight, thanks for the quick reply. I've implemented a similar workaround myself.
Would it make sense to implement such a workaround in mssql-django
? This way the MSSQL driver behaves in a more similar way to Django's officially supported DB drivers.
Thank you for the suggestion! We generally don't do any type conversions on our end but might consider this in the future.
We have this same problem. We were using a postgresql database for years with no issue, but now that we're using mssql we're running into this error. Changing our bulk_create to be one at a time when certain fields are allowed to be NULL is not a great work-around.
Software versions
Table schema and Model
Database Connection Settings
Problem description and steps to reproduce When using
bulk_create
to create multiple price lists at once, we sometime receive the following error message from the db driver:('22003', '[22003] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Arithmetic overflow error converting nvarchar to data type numeric. (8115) (SQLExecDirectW)')
.At first, it seems we might just provide wrong values to the
bulk_create
, but after some further investigation I'm afraid the issue lies with the driver.Business context: Not all items have a medium or best price, so these fields are nullable. The m2 prices are not provided as parameters in the mutation, instead they're automatically calculated by a model manager based on the item's width.
While debugging we noticed something strange. If we provide
bulk_create
with multiple objects where all medium or best prices arenull
, everything works as expected. If we providebulk_create
with multiple objects where all medium or best prices are notnull
, everything also works as expected. However, as soon as we have one object where medium or best price is null, and another object where medium or best is not null we run into the above mentioned error.I.e. the following input will result in an Arithmetic overflow error:
Expected behavior and actual behavior We expect
bulk_create
to also work on the above mentioned input. In development, our tests pass for this input when using a postgres database, howver, the test fails on our CI where we use mssql.Error message/stack trace![image](https://github.com/microsoft/mssql-django/assets/59312283/5c69e468-6cd3-4898-96a5-dbb32c6cf102)
Variables at moment of error![image](https://github.com/microsoft/mssql-django/assets/59312283/72455bf8-f176-4f12-9f12-e59cdc9ba33b)
Any other details that can be helpful I've tried to see the actual SQL that was created by the driver by starting a trace with the SQL server profiler. I've made two mutations 1 with a mixed nullable input (on the left in image below), and one where medium/best were null for both objects (on the right in image below). The SQL on the left resulted in an Arithmetic overflow error, the SQL on the right doesn't.![image](https://github.com/microsoft/mssql-django/assets/59312283/51818823-8811-476f-97bf-fb52896cc52d)
The text found in the image can be seen below for reference:
Screenshot of the the table in question viewed via ssms:
.