0

我需要帮助,当我尝试在现有数据库中加入选择时出现以下错误 (它来自客户端,我只有读取权限(选择)我无法在任何方面修改它)

可能是模型类中的错误,但我是 Peewee ORM 的新手,我也不知道如何声明这些关系:::

(工作)C:\Users\eguzman\Documents\python-work\asterisk-insert-reminder>python script.py Traceback(最近调用最后):文件“script.py”,第 41 行,在 row.agegendas.CODIGO , row.ageagendas.PER_ID, row.stkpersonas.DOCUMENTO)) AttributeError: 'AgeCitas' 对象没有属性 'stkpersonas'

代码:script.py

import peewee as pw
from classes.model import MySqlDbModel, AgeCitas, AgeAgendas, StkPersonas


class SelectCitasToRemind(MySqlDbModel):

    @staticmethod
    def selectCitasToRemind():
        cit = AgeCitas.alias()
        age = AgeAgendas.alias()
        per = StkPersonas.alias()
        query = (cit.select(
            cit.CIT_ID, cit.AGE_ID, cit.FECHAHORA,
            age.AGE_ID, age.CODIGO, age.CEN_ID, age.PER_ID,
            per.PER_ID, per.DOCUMENTO, per.NOMBRE)
            .join(age, pw.JOIN.LEFT_OUTER, on=(cit.AGE_ID == age.AGE_ID))
            .join(per, pw.JOIN.LEFT_OUTER, on=(age.PER_ID == per.PER_ID))
            .where((cit.CIT_ID.is_null(False))
                   & (cit.PAC_ID.is_null(False))
                   )
            .limit(10)
        )
        return query


 query = SelectCitasToRemind.selectCitasToRemind()

 for row in query:
     print("{} | {} | {} | {} | {} | {}".format(
        row.CIT_ID, row.AGE_ID, row.FECHAHORA,
        row.ageagendas.CODIGO, row.ageagendas.PER_ID, row.stkpersonas.DOCUMENTO))

代码:classes/model.py

import peewee as pw


# Conection to mydbname database
dbn = 'mydbname'
user = 'myusername'
pwd = '******'
host = 'localhost'
prt = 3306

db = pw.MySQLDatabase(dbn, user=user,
                  password=pwd,  host=host, port=prt)


class MySqlDbModel(pw.Model):
    class Meta:
        database = db


class AgeCitas(MySqlDbModel):
    CIT_ID = pw.BigIntegerField()
    AGE_ID = pw.BigIntegerField()
    FECHAHORA = pw.DateTimeField()
    ACT_ID = pw.BigIntegerField()
    PAC_ID = pw.BigIntegerField()
    ASIGNADA = pw.CharField()
    IND_ID = pw.BigIntegerField()
    ACUDIO = pw.DoubleField()
    ORDEN = pw.DoubleField()

class Meta:
    table_name = 'AGE_CITAS'


class AgeAgendas(MySqlDbModel):
    AGE_ID = pw.DecimalField()
    CODIGO = pw.CharField()
    CEN_ID = pw.DecimalField()
    PER_ID = pw.DecimalField()
    NPR_ID = pw.DecimalField()
    DESCRIPCION = pw.CharField()
    COLOR = pw.CharField()

class Meta:
    table_name = 'AGE_AGENDAS'


class StkPersonas(MySqlDbModel):
    PER_ID = pw.BigIntegerField()
    DOCUMENTO = pw.CharField()
    NOMBRE = pw.CharField()
    APELLIDO1 = pw.CharField()
    APELLIDO2 = pw.CharField()
    FECHA_ALTA = pw.DateTimeField()
    FECHA_BAJA = pw.DateTimeField()
    EMAIL = pw.CharField()
    TELEFONO1 = pw.CharField()
    TELEFONO2 = pw.CharField()
    TELEFONO3 = pw.CharField()
    TELEFONO4 = pw.CharField()
    SEXO = pw.CharField()
    NACIMIENTO = pw.DateTimeField()
    ES_PACIENTE = pw.CharField()
    ES_PROFESIONAL = pw.CharField()

class Meta:
    table_name = 'STK_PERSONAS'
4

2 回答 2

1

由于您似乎没有实际的外键,因此您需要告诉 peewee 如何通过在连接谓词中提供别名来重建模型图:

       .join(age, pw.JOIN.LEFT_OUTER, on=(cit.AGE_ID == age.AGE_ID).alias('agecitas'))
       .join(per, pw.JOIN.LEFT_OUTER, on=(age.PER_ID == per.PER_ID).alias('sktpersona'))
于 2018-05-18T14:16:34.270 回答
0

如果删除第二个连接,它可以工作,但我真的需要帮助才能使它与两个连接一起工作,但是::

import peewee as pw
from classes.model import BaseModel, AgeCitas, AgeAgendas, StkPersonas


class SelectCitasToRemind(BaseModel):

    @staticmethod
    def selectCitasToRemind():
        cit = AgeCitas.alias()
        age = AgeAgendas.alias()
        query = (cit.select(
            cit.CIT_ID, cit.AGE_ID, cit.FECHAHORA,
            age.AGE_ID, age.CODIGO, age.CEN_ID, age.PER_ID)
            .join(age, pw.JOIN.LEFT_OUTER, on=(cit.AGE_ID ==   age.AGE_ID).alias('agecitas'))
            .where((cit.CIT_ID.is_null(False))
                   & (cit.PAC_ID.is_null(False))
                   )
            .limit(10)
        )
        return query


 query = SelectCitasToRemind.selectCitasToRemind()

for row in query:
    print("{} | {} | {} | {} | {}".format(
        row.CIT_ID, row.AGE_ID, row.FECHAHORA,
        row.agecitas.CODIGO, row.agecitas.PER_ID))

结果:( 工作)C:\Users\eguzman\Documents\python-work\asterisk-insert-reminder-temp>python script.py 744903633820 | 11540 | 2014-02-03 09:00:00 | xxxxx | 10174325 744903633837 | 11540 | 2014-02-05 08:00:00 | xxxxx | 10174325 744903634152 | 11540 | 2014-02-17 15:00:00 | xxxxx | 10174325 744903634179 | 11540 | 2014-02-20 15:10:00 | xxxxx | 10174325 744903634224 | 11540 | 2014-01-30 17:00:00 | xxxxx | 10174325 744903634491 | 11560 | 2014-02-18 14:15:00 | xxxxx | 10174353 744903634541 | 11540 | 2014-03-06 09:40:00 | xxxxx | 10174325 744903634753 | 11620 | 2014-04-10 08:30:00 | xxxxx | 11077145 744903634976 | 11640 | 2014-04-21 08:30:00 | xxxxx | 11077165 744903634979 | 11640 | 2014-04-21 09:15:00 | xxxxx | 11077165

但我真的需要第二次加入,我怎样才能使它与两个加入一起工作???

.join(age, pw.JOIN.LEFT_OUTER, on=(cit.AGE_ID == age.AGE_ID).alias('agecitas'))
.join(per, pw.JOIN.LEFT_OUTER, on=(age.PER_ID == per.PER_ID).alias('sktpersona'))

并做:

for row in query:
print("{} | {} | {} | {} | {} | {}".format(
    row.CIT_ID, row.AGE_ID, row.FECHAHORA,
    row.agecitas.CODIGO, row.agecitas.PER_ID, row.sktpersona.DOCUMENTO))

谢谢

于 2018-05-18T17:11:49.180 回答