获取'数据类型nvarchar(max)和ntext在等于运算符中是不兼容的。

时间:2015-12-01 16:46:51

标签: 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和reference。

我正在使用python 3.4,django 1.8.4和SQL Server 2014

非常感谢任何见解。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题并开启登录sql server以查看发生了什么。看起来长文本字段正在转换为ntext。然后将其与导致错误的nvarchar字段进行比较。

在get_or_create函数中的SELECT期间发生错误。而不是使用get_or_create,使用startswith查询您的模型。使用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
    )