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

**URL:** https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782
**Category:** Developer
**Created:** [December 14, 2023, 11:34pm UTC](https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782 "2023-12-14T23:34:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rvong](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/3bc359/32.png) [@rvong](https://discuss.tryton.org/u/rvong)
#### Post date: [December 14, 2023, 11:34pm UTC](https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782/1 "2023-12-14T23:34:30Z")

</div>

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

```auto
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:

```auto
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?

---

<div class="post-metadata">

### Author: ![rvong](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/3bc359/32.png) [@rvong](https://discuss.tryton.org/u/rvong)
#### Post date: [December 15, 2023, 4:18am UTC](https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782/2 "2023-12-15T04:18:02Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [December 15, 2023, 9:05am UTC](https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782/3 "2023-12-15T09:05:51Z")

</div>

Your user is not allowed in this company.

---

<div class="post-metadata">

### Author: ![rvong](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/3bc359/32.png) [@rvong](https://discuss.tryton.org/u/rvong)
#### Post date: [December 18, 2023, 12:38pm UTC](https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782/4 "2023-12-18T12:38:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [December 19, 2023, 12:39am UTC](https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782/5 "2023-12-19T00:39:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)
#### Post date: [December 22, 2023, 11:48pm UTC](https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782/6 "2023-12-22T23:48:55Z")

</div>

> [@rvong](#):
>
> 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.

Any idea what could be a good addition to the [company modules documentation for user](https://docs.tryton.org/projects/modules-company/en/latest/design.html#user)?

---

<div class="post-metadata">

### Author: ![udono](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/udono/32/2114_2.png) [@udono](https://discuss.tryton.org/u/udono)
#### Post date: [January 2, 2024, 9:02am UTC](https://discuss.tryton.org/t/proteus-searching-for-an-account-account-model-generates-strange-sql-statement/6782/7 "2024-01-02T09:02:59Z")

</div>

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](https://foss.heptapod.net/tryton/tryton/-/blob/branch/default/modules/account/tests/scenario_account_active.rst?plain=1#L23) uses

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

```

[Looking up its definition shows the required proteus steps](https://foss.heptapod.net/tryton/tryton/-/blob/branch/default/modules/company/tests/tools.py) to create a company and link it with the user.
