Proteus searching for an account.account model generates strange SQL statement

I was working with Proteus to fetch some accounts from the DB. Here is the code:

def find_model(model_str, code, key='code', clause=None):
    AModel = Model.get(model_str)
    domain = [(key, "=", code)]
    if clause:
        domain.extend(clause)
    objs = AModel.find(domain, limit=1)
    if objs:
        return objs[0]
    return None

def main():
    company = find_model("company.company", "Some company", 'name')
    with config.get_config().set_context(company=company.id):
        acc = find_model("account.account", "100-500")
   ....

acc returns None, but I know this account exists, so it should return the account. Running the above code with logging enabled, I see that the generated SQL statement for the search has the following WHERE clause:

WHERE ((("a"."code" = \'100-500\')) AND ((false))) ORDER BY "a"."code" ASC, "a"."name" ASC, "a"."id" ASC LIMIT 1

There is a additional AND ((false)) statement that is the cause that no records are returned, and I don’t know where is being generated or why. Does anyone know where the problems is?

I dug a little bit deeper and found out that the cause of this AND ((false)) statement may be due to the a call to Rule.domain_get(cls.__name__) in ModelSQL.__search_query(); and for the account.account, it generates a domain clause like ['company', 'in', []] which might eventually evaluates to False. How do I avoid this call from generating this clause?

In the my sample code, I have added the company that I want to the context, but that didn’t work. So my guess is that I have to set the user’s current company too? How do I do that?

Your user is not allowed in this company.

Thank you for the tip. It appears that not only the user needs to have the company added to his list of companies in order to have access, but also, the user’s current company needs to be set. Maybe a little bit of documentation would be nice to have to know which models depend on ir.user → companies and company fields and which depends on the context company to be set. It is sometimes a bit confusing, but I have figured out.

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

Any idea what could be a good addition to the company modules documentation for user?

The Tryton scenario tests are utilizing proteus. So you can find many proteus examples per module in the scenario tests of each module.

A company setup is needed in many scenarios. E.g. one of the account scenarios uses

create_company(party=None, currency=None, config=None)

Looking up its definition shows the required proteus steps to create a company and link it with the user.

1 Like