django.db.utils.ProgrammingError:运算符不存在:字符变化=整数

时间:2016-12-08 15:07:51

标签: python django postgresql

当我在服务器上运行脚本时,我很需要你的帮助来弄清楚为什么会出现这个错误。服务器运行一个postgres数据库我认为它与迭代的数据的对象类型有关,但不知道如何解决这个问题。

Traceback (most recent call last):
  File "scripts/change_topup.py", line 58, in <module>
    main()
  File "scripts/change_topup.py", line 55, in main
    process_file(path)
  File "scripts/change_topup.py", line 45, in process_file
    for enrolment in enrolments:
  File "/opt/cv_instances/cv1/autodeploy/branches/nboreports/.venv/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
    self._fetch_all()
  File "/opt/cv_instances/cv1/autodeploy/branches/nboreports/.venv/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/opt/cv_instances/cv1/autodeploy/branches/nboreports/.venv/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
    results = compiler.execute_sql()
  File "/opt/cv_instances/cv1/autodeploy/branches/nboreports/.venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "/opt/cv_instances/cv1/autodeploy/branches/nboreports/.venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/opt/cv_instances/cv1/autodeploy/branches/nboreports/.venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/opt/cv_instances/cv1/autodeploy/branches/nboreports/.venv/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/opt/cv_instances/cv1/autodeploy/branches/nboreports/.venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: operator does not exist: character varying = integer
LINE 1: ...n"."type" = 1 AND "registration_household"."name" IN (SELECT...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

我的脚本在这里

# script to update hh top-up locations
import django
django.setup()


import sys
import sys
import xlrd
from django.db import transaction
from maidea.apps.enrolment.models import Enrolment
from maidea.apps.redemption.models import Retailer
from maidea.apps.registration.models import Household
from maidea.apps.interventions.models import Intervention
from maidea.apps.wfp.models import Office


office = Office.objects.get(slug='so-co')
intervention = Intervention.objects.get(project__office=office, slug='cash-for-dadaab-returnees')

print("Using Office: {0}".format(office))
print("Using Intervention: {0}".format(intervention))

def process_file(path):
    # get the first worksheet
    book = xlrd.open_workbook(path)
    first_sheet = book.sheet_by_index(0)
    print('Sheet name: {0}'.format(first_sheet.name))

    print("Looping through excel rows. Please wait ...")
    #counter = 0

    with transaction.atomic():
        # iterate through rows
        for row_idx in range(1, first_sheet.nrows):
            # household names
            hh_name = str(first_sheet.row_values(row_idx)[0])
            # the new top-up locations
            new_top_up = str(first_sheet.row_values(row_idx)[2])


            households = Household.objects.filter(name__in=hh_name)
            top_up = Retailer.objects.filter(name__in=new_top_up) 
            enrolments = Enrolment.objects.filter(intervention=intervention, household__name__in=households) 
            #print("Fetched {0} enrolments".format(enrolments.count())
            for enrolment in enrolments:
                print enrolment
                # enrolment.retailer = top_up
                # enrolment.save()
                #counter += 1
            print("{0} enrolments updated".format(counter))


def main():
    path = "/opt/cv_instances/cv1/autodeploy/branches/nboreports/maidea/somalia/data_import_files/Enrolment.xlsx"
    process_file(path)
    print("DONE PROCESSING FILE.")

main()

已处理的文件包含

等行

HH |充值|

SD803-11H47782 | MERCY US NEW POS TOPUP

SD803-09H03991 | DRC-MOG

帮助将非常感谢。谢谢

1 个答案:

答案 0 :(得分:1)

问题在于这一行

enrolments = Enrolment.objects.filter(intervention=intervention, household__name__in=households):

household是Household个实例的查询集,而不是名称。我认为以下任何一种都可行。

enrolments = Enrolment.objects.filter(intervention=intervention, household__in=hh_name) 


enrolments = Enrolment.objects.filter(intervention=intervention, household__name__in=hh_name) 

如果您使用第二个,那么您可以从代码中删除households查询集。