sql-server - 获取 'The data types nvarchar(max) and ntext are incompatible in the equal to operator.'

标签 sql-server django python-3.x

我正在尝试用数据填充表并使用 Django 的 get_or_create 方法。每当我这样做时,它都会将记录输入数据库,但在某个记录处它会抛出上述错误。我的查询集函数是

r, created = Response.objects.get_or_create(
    auth_user=auth_user,
    name=surv_name,
    organization=org_id,
    category=category,
    question=question,
    present_order=present_order,
    reference=reference,
    quest_id=quest_id,
    survey_id=survey_id
)

我的回复表是

class Response(models.Model):
    auth_user = models.ForeignKey('AuthUser')
    survey = models.ForeignKey('Survey')
    name = models.CharField(max_length=50)
    organization = models.ForeignKey('Organization')
    tf_question_key = models.CharField(max_length=50)
    category = models.CharField(max_length=25, blank=True, null=True)
    question = models.CharField(max_length=2048)
    quest_id = models.CharField(max_length=25)
    present_order = models.IntegerField()
    reference = models.CharField(max_length=20)
    answer = models.CharField(max_length=2048)
    remediation = models.CharField(max_length=2048, blank=True, null=True)
    dt_started = models.DateTimeField(db_column='DT_Started',
        auto_now_add=True)  # Field name made lowercase.
    dt_completed = models.DateTimeField(db_column='DT_COMPLETED',
        auto_now_add=True)  # Field name made lowercase.


    class Meta:
        managed = False
        db_table = 'response'

错误所在的回溯是

organization  <Organization: Individual Offices>
r  <Response: Response object>
user_id  2
question  ('Does your written policy include the follow-up process for significant outstanding checks, including, but not limited to, checks to recording clerk, checks to tax collector, hazard insurance checks, underwriter checks or checks for mortgage payoffs and any other high risk items? ( 2.03 k )')
present_order  21
survey_id  1
reference  '2.03 (k)'
quest_id  27
created  True
category  'Pillar II'
surv_name  'Compliance Benchmark'
org_id  1
auth_user  <AuthUser: AuthUser object>

我可以使用

将记录添加到表中
r = Response(
    auth_user=auth_user,
    name=surv_name,
    organization=organization,
    category=category,
    question=question,
    present_order=present_order,
    reference=reference,
    quest_id=quest_id,
    survey_id=survey_id
)
r.save()

但我需要使用 get_or_create 方法来避免重复记录。我不确定为什么我可以使用 .save() 方法添加记录,但不能使用 get_or_create 以及为什么使用 get_or_create 它将添加记录到某一记录然后失败。唯一发生变化的是问题、quest_id、present_order 和引用。

我使用的是 python 3.4、django 1.8.4 和 SQL Server 2014

任何见解将不胜感激。

最佳答案

我遇到了同样的问题,并打开 SQL Server 上的日志记录以查看发生了什么。看起来长文本字段正在转换为 ntext。然后将其与导致错误的 nvarchar 字段进行比较。

在 get_or_create 函数中执行 SELECT 期间发生错误。使用startswith 查询模型,而不是使用get_or_create。使用startswith执行LIKE检查,这将起作用。我还在字段上添加了长度检查,以确保字段匹配,而不是查找具有相同起始值的其他行。

from django.core.exceptions import ObjectDoesNotExist
from django.db.models.functions import Length

attrs = {
    auth_user=auth_user,
    name=surv_name,
    organization=org_id,
    category=category,
    present_order=present_order,
    reference=reference,
    quest_id=quest_id,
    survey_id=survey_id,
}

try:
    r = Response.objects.annotate(
        text_len=Length('question')
    ).get(
        text_len__exact=len(question),
        question__startswith=question,
        **attrs
    )
except ObjectDoesNotExist:
    r = Response.objects.create(
        question=question,
        **attrs
    )

关于sql-server - 获取 'The data types nvarchar(max) and ntext are incompatible in the equal to operator.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34025565/

相关文章:

sql - 是否可以在数据库中找到所有具有系统生成名称的主键?

python - 如何在 Django ImageField 中验证图像格式

python - sys.stdin 和 input() 之间的冲突 - EOFError : EOF when reading a line

sql - 只在sql server的列中插入日期

sql-server - 授予某个数据库中所有存储过程的执行权限

sql - case when 语句中按列号对表进行排序

python - Django 在重定向时找不到模式名称

python - django外键保存文件名而不是对象

python - 为什么我在 scrapy 中收到此错误 - python3.7 语法无效

python pip - 具有本地依赖项的开发模式