SQL Alchemy 数据类型 text 和 text 在相等运算符中不兼容

SQL Alchemy the data types text and text are incompatible in the equal operator

在我的 django API 的一部分中,我有以下内容来更新旧笔记:

old_note = request.databaseSession.query(Tmemo).\
    filter(Tmemo.memosern1 == serial).\
    one()

这个查询没有任何问题,因为我在许多其他地方使用它。奇怪的问题是:

old_note.memotext = newtext

然后当我提交以下错误时:

ProgrammingError: (pyodbc.ProgrammingError) ('42000', u'[42000] 
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The data types 
text and text are incompatible in the equal to operator. (402) 
(SQLExecDirectW)') [SQL: u'UPDATE tmemo SET memotext=? WHERE 
tmemo.memosern1 = ? AND tmemo.memosern2 = ? AND tmemo.memotype = ? AND 
tmemo.memotext = ?'] [parameters: ('asassasasasaassaassaasas2121', 
u'P03000000060445', u'MEMO', u'5', u'asassasasasaassaassaasas')] 
(Background on this error at: http://sqlalche.me/e/f405)

Tmemo 只是一个 table,带有序列号 memosern1memosern2 的列,键入 memotype 并且注释本身为 memotext, serials 和 type 是 varchar 和 memotext 作为文本。我真的不明白text and text are incompatible是什么意思真的,没有意义,而且我只是过滤序列号,没有别的,所以这个奇怪的查询是怎么回事?

而且我可以使用完全相同的变量在 table 中完美地创建另一行 (newtext),我只是无法更新现有的。

有什么想法吗?我可以提供更多信息

TEXT 类型实际上只是一个 BLOB 并且可能非常大,具体取决于您的数据库服务器 2**64-1 字节甚至更多。由于其(可能的)大小,TEXTBLOB 未实现相等运算符,因此您无法将类型 TEXT 的值与任何值(在您的情况下 TEXT to TEXT).

您需要做的是使用 VARCHAR 或 - 或者,稍微 hacky - 计算并存储 TEXT 列的哈希值并进行比较。如果您计算 TEXT 值的 MD5(),仅将高 64 位存储为 INT 并在该列上放置一个功能索引,您基本上可以获得相同的结果而不会太多成本(除了 insert/update 上的额外散列 + 索引操作,以及你将某些东西与并不真正相等的相等进行比较的可能性很小)。

从长远来看,您应该使用 VARCHAR 而不是 TEXT,因为 TEXT 将是 removed from future versions of SQL Server.