Django 1.11.2加载数据查询有错误:Django.db.utils文件.ProgrammingError:关系“eventtransactions”不存在

2024-09-24 00:31:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个Django查询来从数据库加载数据。当我这样做的时候,我得到一个错误,上面写着:

django.db.utils.ProgrammingError: relation "eventtransactions" does not exist
LINE 1: ...sactions"."eventtransactions_id") AS "count" FROM "eventtran... 

我想创建一个循环,计算每个成员类型每天的事件事务数,并将其存储在字典中,其中键是membertype,值是该成员类型当天的事件事务数。你知道吗

我试过修改正在计算的内容,但遇到了一些问题。我试过按membertype和custnum计数,但仍然得到相同的编程错误。你知道吗

健身模式:

  class EventTransactions(models.Model):

    eventtransactions = models.BigAutoField(primary_key=True)
    eventtransactions_id = models.IntegerField()
    profitcenter_id = models.IntegerField()
    customer_gender = models.TextField()
    customer_firstname = models.TextField()
    customer_lastname = models.TextField()
    actualdatetime = models.DateTimeField(blank=True, null=True)
    custnum = models.BigIntegerField(blank=True, null=True)
    birthdate = models.DateField(blank=True, null=True)
    membertype = models.TextField(blank=True, null=True)
    eventname = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'eventtransactions'

导入\u刷卡_计数.py你知道吗

import psycopg2
import redis
import datetime
import json
from optparse import make_option

from django.core.management.base import BaseCommand, CommandError
from urec.settings import REDIS_HOST, REDIS_DB

from django.db.models import Count
from django.db.models.functions import TruncDay
from fitness.models import EventTransactions


# the inverse mapping between the excessive membership types and the compress categories.
MEM_MAP_INV = {
    'alumni': ['Alum 1yr Fam','Alum 1yr Ind','Alum 3mo Fam','Alum 3mo 
Ind','Alum 6mo Fam','Alum 6mo Ind','Alumni'],
    'corporate': ['Corp 1yr Fam','Corp 1yr Ind','Corp 3mo Fam','Corp 3mo Ind','Corp 6mo Fam','Corp 6mo Ind','Corporate'],
    'retiree': ['Retiree','Ret 1yr Fam','Ret 1yr Ind','Ret 3mo Fam','Ret 3mo Ind','Ret 6mo Fam','Ret 6mo Ind','Ret 9mo Fam','Ret 9mo Ind'],
    'student': ['Student',,'CMU Stud 1/2','waiver stu','MMCC','1/2MMCC'],
    'staff': ['Faculty/Staff', 'Faculty','Staff','PE STAFF','Athletic Staff','F/S 1yr Fam','F/S 3mo Fam','F/S 3mo Ind','F/S 6mo Fam','F/S 9mo Fam','F/S 9mo Ind','F/S Ind 1 yr','F/S Ind 6mo','F/S','F/S Fam'],
    'donor': ['Donor'],
    'volunteer': ['Volunteer','Volunteer-1yr'],
    'community ltd': ['Community Ltd.','C/L 1 yr Ind','C/L 1yr Fam','C/L 3mo Fam','C/L 3mo Ind','C/L 6mo Fam','C/L 6mo Ind'],
    'other': ['Other','Other 1yr Ind','Not Required','One Month','N/A'],
    'n/a': [None]
}

class Command(BaseCommand):
    """
        Gets the swipe data from the fitnessdb and counts the daily swipes
        by membership.

        This count information is put into redis.  The key has the 
form 'urec:counts:<profitcenter_id>:<YYYY-mm-dd>'.

        The value is a json string in the form '{membertype1: count1, membertype2: count2, ...}'.

        By default, counts are gotten for the previous day. Example specifying date range:

        ./manage.py import_swipe_counts --startdate='2014-07-08' --enddate='2014-07-10'

    """

    help = 'Gets counts of users from the swipe data, and dumps it in Redis.'

    # Named (optional) arguments
    def add_arguments(self, parser):
        today = datetime.datetime.now().date()
        yesterday = (today - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        today = today.strftime('%Y-%m-%d')

        parser.add_argument('-s', '--startdate',
            dest='start_date',
            default=yesterday,
            help='Start date of data importing.')
        parser.add_argument('-e', '--enddate',
            dest='end_date',
            default=today,
            help='End date of data importing.')
        parser.add_argument('--profitcenterid',
            dest='profitcenter_id',
            default=7,
            help='Profit center id. Default is 7.')

    def handle(self, *args, **options):

        r = redis.Redis(host=REDIS_HOST, db=REDIS_DB)

        memberships = MEM_MAP_INV.keys()

        start_date = datetime.datetime.strptime(options['start_date'],'%Y-%m-%d').date()
        end_date = datetime.datetime.strptime(options['end_date'],'%Y-%m-%d').date()

        profitcenter_id = options['profitcenter_id']

         # number of days passed
         elapsed_days = (end_date - start_date).days

        dates = [start_date + datetime.timedelta(days=i) for i in range(elapsed_days)]

        counts = list(EventTransactions.objects.annotate(day=TruncDay('actualdatetime')).values('day','membertype').annotate(count=Count('eventtransactions_id')).filter(actualdatetime__gte=start_date).filter(actualdatetime__lte=end_date))

        # prepare for redis
        r_key = 'urec:counts:%s:%s' % (profitcenter_id, str(date))
        counts = json.dumps(counts)
        r.set(r_key,counts)

Tags: thefromimportidtruedatetimedatemodels