Using Django 2.X, and Pyodbc driver (installed from anaconda conda-forge django-pyodbc-azure
) against MS Sql Server (not sure which version), I regularly have bugs using prefetch_related
. An example looks simply like:
for obj in MyORMType.objects.prefetch_related('otherormtype_set').all():
pass
where OtherOrmType
has a simple foreign key to MyOrmType
, the error is:
...
/opt/conda/lib/python3.6/site-packages/django/db/backends/utils.py in _execute(self, sql, params, *ignored_wrapper_args)
83 return self.cursor.execute(sql)
84 else:
---> 85 return self.cursor.execute(sql, params)
86
87 def _executemany(self, sql, param_list, *ignored_wrapper_args):
/opt/conda/lib/python3.6/site-packages/sql_server/pyodbc/base.py in execute(self, sql, params)
544 self.last_params = params
545 try:
--> 546 return self.cursor.execute(sql, params)
547 except Database.Error as e:
548 self.connection._on_error(e)
ProgrammingError: ('The SQL contains -2098 parameter markers, but 128974 parameters were supplied', 'HY000')
I can fall back to the dumb equivalent:
for obj in MyORMType.objects.all():
other_objs = obj.otherormtype_set.all()
but this is obviously quite slow. This bug occurs regularly for me under many different circumstances in this particular setup (always same Django version, driver, and DB), it's not a one-off annoyance. Is this my fault or an issue with SQL Server or Pyodbc (or Django)? Is there a way to work around the error without having to fetch each obj.otherormtype_set
one at a time?