pyodbc icon indicating copy to clipboard operation
pyodbc copied to clipboard

Django, Invalid precision value (0) (SQLBindParameter) when saving character to Textfield

Open leezimu516 opened this issue 5 years ago • 3 comments

Please first make sure you have looked at:

  • Documentation: https://github.com/mkleehammer/pyodbc/wiki
  • Other issues

Environment

To diagnose, we usually need to know the following, including version numbers. On Windows, be sure to specify 32-bit Python or 64-bit:

  • Python: 3.6
  • pyodbc 4.0.28:
  • OS windows 10 64-bit:
  • DB sql server 2014:
  • driver: ODBC Driver 13 for SQL Server
  • Django: 2.1.10

Issue

When I try to insert over 3000 chars into textfield, it throws an Invalid precision value error. However pyodbc 4.0.23 works fine.

Django error

Traceback (most recent call last):
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
    return self.cursor.execute(sql, params)
pyodbc.Error: ('HY104', '[HY104] [Microsoft][ODBC Driver 13 for SQL Server]Invalid precision value (0) (SQLBindParameter)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\contrib\admin\options.py", line 604, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\contrib\admin\sites.py", line 223, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\contrib\admin\options.py", line 1640, in change_view
    return self.changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\utils\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\utils\decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\contrib\admin\options.py", line 1525, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\contrib\admin\options.py", line 1564, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\contrib\admin\options.py", line 1091, in save_model
    obj.save()
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\models\base.py", line 718, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\models\base.py", line 748, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\models\base.py", line 812, in _save_table
    forced_update)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\models\base.py", line 861, in _do_update
    return filtered._update(values) > 0
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\models\query.py", line 712, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\models\sql\compiler.py", line 1383, in execute_sql
    cursor = super().execute_sql(result_type)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\models\sql\compiler.py", line 1065, in execute_sql
    cursor.execute(sql, params)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\debug_toolbar\panels\sql\tracking.py", line 164, in execute
    return self._record(self.cursor.execute, sql, params)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\debug_toolbar\panels\sql\tracking.py", line 106, in _record
    return method(sql, params)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Developer\AppData\Roaming\Python\Python36\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\site-packages\sql_server\pyodbc\base.py", line 546, in execute
    return self.cursor.execute(sql, params)
django.db.utils.Error: ('HY104', '[HY104] [Microsoft][ODBC Driver 13 for SQL Server]Invalid precision value (0) (SQLBindParameter)')

odbc trace SQL.LOG

leezimu516 avatar Feb 07 '20 21:02 leezimu516

Probably related: #547

gordthompson avatar Feb 08 '20 16:02 gordthompson

Any update or workaround on this? I am facing the same issue when trying to input long string (>30,000 char) into azure sqldb. I have tried defining the columns explicitly to NVARCHAR(max) as well as cursor.setinputsizes([(pyodbc.SQL_WVARCHAR, 0, 0)]) as suggested else where but no avail.

I tried dataframe.to_sql route by defining dtype as sqlalchemy.types.NVARCHAR(max) - Same error as above. Tried simple and straight forward way usin pyodbc cursor cursor.executemany(sqlQry, values) - Same error Tried sqlalchemy route by defining a table with correct column types and inserting - Same error

This is driving me nuts! Any help would be appreciated.

Thanks

amitmadahar avatar Apr 15 '21 19:04 amitmadahar

Seems I can't reproduce your error. Is there any difference from your environment?

> pip freeze
Django==2.1.10
django-pyodbc-azure==2.1.0.0
pyodbc==4.0.28
pytz==2021.1
> python -V
Python 3.6.8

I'm using .objects.create(name='x'*40000) to insert into TextField.

absci avatar Apr 16 '21 17:04 absci

Closing due to lack of activity.

keitherskine avatar Dec 10 '22 15:12 keitherskine

I am facing the same issue. Do you have any solutions for these?

ap-kamal avatar Apr 16 '23 14:04 ap-kamal

@ap-kamal - Please open a new issue and include a minimal reproducible example that illustrates the problem you are facing. That example will look something like this except that it will throw the error. (That code works fine for me.)

gordthompson avatar Apr 16 '23 15:04 gordthompson