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 a function that takes as a parameter a list of "Projects" (list of the model above instances), and calculates a score based on the two fields "creation_date" and "hours_spent", here is the code :
Please note that my creation_date is a nullable field, and in the case of hours_spent, the field is not nullable but there can be >= 0 or <0 values. In my case, the bug occured without the presence of null values in creation_date (omit project D here).
The problem occurs when I'm calling the function to calculate the score of some dataset, an exception occurs specifically in operations.py in the function subtract_temporals
Expected behavior and actual behavior
Expected behavior : a list of Projects with the 3 fields annotated to it (difference_duration, days_difference, score)
Actual behavior : exception occuring when annotating the difference_duration
Error message/stack trace
Traceback (most recent call last):
File "C:\Users\XXX\Documents\Django_projects\myprj\api\views\views_all_projects.py", line 94, in post
p3 = list(score_projects(c1))
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\query.py", line 400, in __iter__
self._fetch_all()
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\query.py", line 1928, in _fetch_all
self._result_cache = list(self._iterable_class(self))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\query.py", line 91, in __iter__
results = compiler.execute_sql(
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\sql\compiler.py", line 1549, in execute_sql
sql, params = self.as_sql()
^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\mssql\compiler.py", line 212, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\sql\compiler.py", line 84, in pre_sql_setup
self.setup_query(with_col_aliases=with_col_aliases)
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\sql\compiler.py", line 73, in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select(
^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\sql\compiler.py", line 296, in get_select
sql, params = self.compile(col)
^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\mssql\compiler.py", line 442, in compile
return super().compile(node, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\sql\compiler.py", line 546, in compile
sql, params = node.as_sql(self, self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\expressions.py", line 1326, in as_sql
return compiler.compile(self.expression)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\mssql\compiler.py", line 442, in compile
return super().compile(node, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\sql\compiler.py", line 546, in compile
sql, params = node.as_sql(self, self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\django\db\models\expressions.py", line 830, in as_sql
return connection.ops.subtract_temporals(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\XXX\Documents\PythonVirtualEnvironments\dc_venv_3.11.7\Lib\site-packages\mssql\operations.py", line 540, in subtract_temporals
params = rhs_params + lhs_params
~~~~~~~~~~~^~~~~~~~~~~~
TypeError: can only concatenate list (not "tuple") to list
Any other details that can be helpful
The problem seems caused by a non-initialized empty list (in this case either one of rhs_params or lhs_params), that instead of providing an empty list [], it's providing an empty tuple (), precisely it occurs in the (if internal_type == 'DateField') statement, here's what I got in my failing test when printing these values :
rhs_params = ['2015-01-01']
lhs_params = ()
I was able to fix this problem simply by editing the function subtract_temporals and adding intialization in the case of empty lists :
Software versions
Table schema and Model
Database Connection Settings
Problem description and steps to reproduce
I have a function that takes as a parameter a list of "Projects" (list of the model above instances), and calculates a score based on the two fields "creation_date" and "hours_spent", here is the code :
Where :
I'm using ExpressionWrapper in order to extend my model by adding some calculated fields :
a dataset example :
Please note that my creation_date is a nullable field, and in the case of hours_spent, the field is not nullable but there can be >= 0 or <0 values. In my case, the bug occured without the presence of null values in creation_date (omit project D here).
The problem occurs when I'm calling the function to calculate the score of some dataset, an exception occurs specifically in operations.py in the function subtract_temporals
Expected behavior and actual behavior
Expected behavior : a list of Projects with the 3 fields annotated to it (difference_duration, days_difference, score)
Actual behavior : exception occuring when annotating the difference_duration
Error message/stack trace
Any other details that can be helpful
The problem seems caused by a non-initialized empty list (in this case either one of rhs_params or lhs_params), that instead of providing an empty list [], it's providing an empty tuple (), precisely it occurs in the (if internal_type == 'DateField') statement, here's what I got in my failing test when printing these values :
I was able to fix this problem simply by editing the function subtract_temporals and adding intialization in the case of empty lists :
The text was updated successfully, but these errors were encountered: