滤波器长度不兼容的比较误差

时间:2017-04-17 22:47:52

标签: python sql-server django django-pyodbc

问题

我在下面详细介绍了一个不兼容的比较错误,但它取决于我传递给过滤器的字符串的大小。任何人都知道这个错误的原因或解决方案是什么,或者我可以深入挖掘以确定根本问题?

详细

当我使用长度为255的字符串查询过滤时,我收到了{I}预期的False响应(与插入的列值不完全匹配):

>>> from core.models import TestTable
>>> test_str = '--publication_filter|920,921,922,923,925,926,927,928,929,930,932,933,934,935,936,937,938,939,940,941,1024,1237,1239,1255,1302,1386,1442,1724,1842,9926,9929,9979,12818,12822,12864,12867,21301,21417,21418,21419,21420,21570,22046,22080,22081,22087,22167,1234'
>>> len(test_str)
255
>>> test1 = TestTable.objects.filter(test_column=test_str)
>>> test1.exists()
False

但是,对于长度为256的字符串,我期望返回True(匹配我插入的列值),它会引发错误(此测试与上面的测试完全相同,但test_str除外是一个字符更长):

注意:我在下面的追溯中修改了我的路径。

>>> from core.models import TestTable
>>> test_str = '--publication_filter|920,921,922,923,925,926,927,928,929,930,932,933,934,935,936,937,938,939,940,941,1024,1237,1239,1255,1302,1386,1442,1724,1842,9926,9929,9979,12818,12822,12864,12867,21301,21417,21418,21419,21420,21570,22046,22080,22081,22087,22167,12345'
>>> len(test_str)
256
>>> test2 = TestTable.objects.filter(test_column=test_str)
>>> test2.exists()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/[REDACTED]/.venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 565, in exists
    return self.query.has_results(using=self.db)
  File "/[REDACTED]/.venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 441, in has_results
    return bool(compiler.execute_sql(SINGLE))
  File "/[REDACTED]/.venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
    cursor.execute(sql, params)
  File "/[REDACTED]/.venv/local/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 325, in execute
    return self.cursor.execute(sql, params)
ProgrammingError: ('42000', '[42000] [FreeTDS][SQL Server]The data types nvarchar and text are incompatible in the equal to operator. (402) (SQLParamData)')

查看并运行原始查询工作正常,这让我对我所在的django-pyodbc包非常怀疑。

>>> str(TestTable.objects.filter(test_column=test_str).query)
'SELECT [custom].[test_table].[test_id], [custom].[test_table].[test_column] FROM [custom].[test_table] WHERE [custom].[test_table].[test_column] = --publication_filter|920,921,922,923,925,926,927,928,929,930,932,933,934,935,936,937,938,939,940,941,1024,1237,1239,1255,1302,1386,1442,1724,1842,9926,9929,9979,12818,12822,12864,12867,21301,21417,21418,21419,21420,21570,22046,22080,22081,22087,22167,12345 '

编辑查询(报价已修复),手动查询我的数据库时返回没有问题:

SELECT [custom].[test_table].[test_id], [custom].[test_table].[test_column]
FROM [custom].[test_table]
WHERE [custom].[test_table].[test_column] = '--publication_filter|920,921,922,923,925,926,927,928,929,930,932,933,934,935,936,937,938,939,940,941,1024,1237,1239,1255,1302,1386,1442,1724,1842,9926,9929,9979,12818,12822,12864,12867,21301,21417,21418,21419,21420,21570,22046,22080,22081,22087,22167,12345'

其他信息

系统

  • 服务器操作系统: Ubuntu 14.04.5 LTS
  • 数据库驱动程序: FreeTDS
  • djanbo-pyodbc https://github.com/avidal/django-pyodbc/tree/django-1.4
    • 注意:由于某些未知原因,我们使用此软件包的本地存储副本构建项目,但这是我相信我们所使用的软件包的分支。

CREATE TABLE [custom].[test_table] (
    test_id INT NOT NULL PRIMARY KEY IDENTITY(1, 1),
    test_column NVARCHAR(4000),
);

INSERT INTO custom.test_table (test_column)
VALUES ('--publication_filter|920,921,922,923,925,926,927,928,929,930,932,933,934,935,936,937,938,939,940,941,1024,1237,1239,1255,1302,1386,1442,1724,1842,9926,9929,9979,12818,12822,12864,12867,21301,21417,21418,21419,21420,21570,22046,22080,22081,22087,22167,12345');

模型

class TestTable(models.Model):
    test_id = models.AutoField(primary_key=True)
    test_column = models.TextField(null=True)
    class Meta:
        db_table = u'custom].[test_table'

1 个答案:

答案 0 :(得分:0)

有几种django-pyodbc种味道,但在我尝试过的包装中,django-pyodbc-azure表现最佳。我建议给它一个镜头:

https://github.com/michiya/django-pyodbc-azure