Alias Error when i use a sql.table class

Bonjour à tous..

J’ai une erreur lorsque j’utilise les alias lors des requêtes sql au niveau de table_query.

Voici mon code détaillée :

@classmethod
    def table_query(cls):

        SalePriceList = Pool().get('product.price_list')
        sale_price_list = SalePriceList.__table__()

        Invoice = Pool().get('account.invoice')
        invoice = Invoice.__table__()

        print(type(invoice))
        i1 = invoice("i1")
        i2 = invoice("i2")
        i3 = invoice("i3")

        # Jointures croisant facture et avoir
        join_ref = Join(i1, i2, 'LEFT')
        join_ref.condition = i1.number == i2.reference

        join_rev = Join(join_ref, i3, 'LEFT')
        join_rev.condition = join_ref.left.reference == join_rev.right.number

        # Jointure pour aller chercher sale_price_list.name
        join_spl = Join(join_ref.left, sale_price_list, 'LEFT')
        join_spl.condition = join_ref.left.sale_price_list == sale_price_list.id

        # Clause de base
        where = Literal(True)
        ctx = Transaction().context

        if ctx.get('start_date'):
            where &= i1.invoice_date >= ctx['start_date']
        if ctx.get('end_date'):
            where &= i1.invoice_date <= ctx['end_date']
        where &= i1.state.in_(['paid', 'posted'])

        # Élimination des factures avec relation crédit/avoir
        where &= (i2.id == None)  # i1.number ≠ i2.reference
        where &= (join_rev.right.id == None)  # i1.reference ≠ i3.number

        print(join_ref.left.sale_price_list.name)
        return join_rev.select(
            join_spl.right.name,
            Sum(join_ref.left.montant_assurance),
            where=where,
            group_by=[join_spl.right.name]
        )

Lorsque j’exécute celui-ci j’ai cette erreur

i1 = invoice.alias('i1')
         ^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object is not callable

Naturellement invoice est de class sql.table et cette classe n’a pas pour attribut alias. OK pour ceci.

J’essaye donc de remplacer par

print(type(invoice))
        i1 = invoice 
        i2 = invoice 
        i3 = invoice 

Mais j’ai cette erreur ci

cursor.execute(self, sql, args)
psycopg2.errors.DuplicateAlias: table name "b" specified more than once

Je n’ai plus vraiment d’idée si vous pouvez m’aider s’il vous plait je vous en remercie.

tu devrais essayer avec:

Invoice = Pool().get('account.invoice')
i1 = Invoice.__table__()
i2 = Invoice.__table__()
i3 = Invoice.__table__()

Avec python-sql il ne faut pas s’occuper des alias, la librairie en génère un unique pour chaque instance de Table.

Merci à vous

J’ai appliqué comme vous me l’avez dit. Et j’ai corrigé ma fonction que voici

class Classement_Assurance_vente(ModelSQL, ModelView):
    "Classement des ventes Totale par Assurance"
    __name__ = "ventes.assurances"

    _rec_name = "Ventes Assurances"
    _history = False

    assurance_name = fields.Char("Assurance")
    total_vente = fields.Float("Total des ventes.")


    @classmethod
    def table_query(cls):

        SalePriceList = Pool().get('product.price_list')
        spl1 = SalePriceList.__table__()

        Party = Pool().get('party.party')
        party_all1 = Party.__table__()

        PartySalePriceList = Pool().get('party.party.sale_price_list')
        partySPL1 = PartySalePriceList.__table__()

        Invoice = Pool().get('account.invoice')
        invoice = Invoice.__table__()

        print(type(invoice))
        i1 = Invoice.__table__()
        i2 = Invoice.__table__()
        i3 = Invoice.__table__()

        # Jointures croisant facture et avoir
        join_ref = Join(i1, i2, 'LEFT')
        join_ref.condition = i1.number == i2.reference

        join_rev = Join(join_ref, i3, 'LEFT')
        join_rev.condition = join_ref.left.reference == join_rev.right.number

        # JOINTURE ENTRE PARTY_PARTY_SALE_PRICE_LIST ET PRODUCT_PRICE_LIST
        join_ppspl = Join(partySPL1, spl1, 'LEFT')
        join_ppspl.condition = partySPL1.sale_price_list == spl1.id

        # Jointure Party et join_ppspl
        join_part = Join(party_all1, join_ppspl, 'LEFT')
        join_part.condition = party_all1.id == partySPL1.id

        # Jointure entre join_part et invoice maintenant
        join_v = Join(join_rev, join_part, 'LEFT')
        join_v.condition = join_ref.left.party == join_part.left.id
        
        # Clause de base
        where = Literal(True)
        ctx = Transaction().context

        if ctx.get('start_date'):
            where &= i1.invoice_date >= ctx['start_date']
        if ctx.get('end_date'):
            where &= i1.invoice_date <= ctx['end_date']
        where &= i1.state.in_(['paid', 'posted'])

        # Élimination des factures avec relation crédit/avoir
        where &= (i2.id == None)  # i1.number ≠ i2.reference
        where &= (join_rev.right.id == None)  # i1.reference ≠ i3.number

        print(join_ref.left.sale_price_list.name)
        return join_v.select(
            Literal(1).as_('id'),
            spl1.name.as_('assurance_name'),
            Sum(join_ref.left.montant_assurance).as_('total_vente'),
            Cast(Now(), 'timestamp').as_('create_date'),
            Literal(1).as_('create_uid'),
            Cast(None, 'timestamp').as_('write_date'),
            Literal(None).as_('write_uid'),
            where=where,
            group_by=[spl1.name]
        )

Maintenant à la fin j’obtiens cette erreur d’access et droits

Traceback (most recent call last):
  File "/gnuhealth/tryton/server/trytond-6.0.55/trytond/wsgi.py", line 117, in dispatch_request
    return endpoint(request, **request.view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gnuhealth/tryton/server/trytond-6.0.55/trytond/protocols/dispatcher.py", line 46, in rpc
    return methods.get(request.rpc_method, _dispatch)(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gnuhealth/tryton/server/trytond-6.0.55/trytond/wsgi.py", line 84, in auth_required
    return wrapped(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gnuhealth/tryton/server/trytond-6.0.55/trytond/protocols/wrappers.py", line 181, in wrapper
    return func(request, pool, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gnuhealth/tryton/server/trytond-6.0.55/trytond/protocols/dispatcher.py", line 180, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/gnuhealth/tryton/server/trytond-6.0.55/trytond/model/modelsql.py", line 801, in read
    raise RuntimeError("Undetected access error")
RuntimeError: Undetected access error

J’ai l’impression que ça vient du fait que j’ai ajouter les champ create_date etc dans mon return à la fin.
Mais en enlevant ces champs, j’ai une erreur disant qu’il faut les mettre.

Des astuces s’il vous plait?

l’id doit être différent pour chaque ligne retournée. Il faut donc le récupérer à partir d’une des tables de la requête ou le générer.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.