# Do not call a field 'name' inside your Many2Many model

**URL:** https://discuss.tryton.org/t/do-not-call-a-field-name-inside-your-many2many-model/5797
**Category:** Developer
**Created:** [January 4, 2023, 7:12pm UTC](https://discuss.tryton.org/t/do-not-call-a-field-name-inside-your-many2many-model/5797 "2023-01-04T19:12:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [January 4, 2023, 7:12pm UTC](https://discuss.tryton.org/t/do-not-call-a-field-name-inside-your-many2many-model/5797/1 "2023-01-04T19:12:14Z")

</div>

Just for information if anybody also stumbles upon it.

Today I upgraded a Tryton instance from 5.8 to 6.6, which went smooth until the indexes were created / updated. In a custom module I had a Many2Many relation and added a Many2Many model with the two fields. One of the fields I had called `name` which is a Many2One field. During updating Tryton failed to go by this model and always failed. Even creating a new empty database and trying to initialize it didn’t work. With that I realized there was something wrong with my code and renaming the field fixed this issue.

So in short:

```python
class RelatedData(ModelSQL):
    'demo not to use a "name" field in this model'
    __name__ = 'demo.related-res.user'

    name = fields.Many2One('demo.related'. 'Demo') # BAD!!!! use another name for the field!
    user = fields.Many2One('res.user', 'User')

```

This will trigger the error when creating the indexes.

---

<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: [January 4, 2023, 9:54pm UTC](https://discuss.tryton.org/t/do-not-call-a-field-name-inside-your-many2many-model/5797/2 "2023-01-04T21:54:09Z")

</div>

> [@edbo](#):
>
> This will trigger the error when creating the indexes.

Which error? Can you provide the traceback?

---

<div class="post-metadata">

### Author: ![edbo](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@edbo](https://discuss.tryton.org/u/edbo)
#### Post date: [January 4, 2023, 10:15pm UTC](https://discuss.tryton.org/t/do-not-call-a-field-name-inside-your-many2many-model/5797/3 "2023-01-04T22:15:58Z")

</div>

> [@ced](#):
>
> Which error? Can you provide the traceback?

```auto
>>>> INFO trytond.modules index:create demo.related-res.user
Traceback (most recent call last):
  File "/srv/basesystem/bin/trytond-admin", line 31, in <module>
    admin.run(options)
  File "/srv/basesystem/lib64/python3.9/site-packages/trytond/admin.py", line 57, in run
    pool.init(update=options.update, lang=list(lang),
  File "/srv/basesystem/lib64/python3.9/site-packages/trytond/pool.py", line 165, in init
    restart = not load_modules(
  File "/srv/basesystem/lib64/python3.9/site-packages/trytond/modules/ __init__.py", line 449, in load_modules
    _load_modules(update)
  File "/srv/basesystem/lib64/python3.9/site-packages/trytond/modules/ __init__.py", line 417, in _load_modules
    load_module_graph(graph, pool, update, lang)
  File "/srv/basesystem/lib64/python3.9/site-packages/trytond/modules/ __init__.py", line 300, in load_module_graph
    model._update_sql_indexes()
  File "/srv/basesystem/lib64/python3.9/site-packages/trytond/model/modelsql.py", line 482, in _update_sql_indexes
    table_h.set_indexes(cls._sql_indexes)
  File "/srv/basesystem/lib64/python3.9/site-packages/trytond/backend/postgresql/table.py", line 469, in set_indexes
    cursor.execute(
  File "/srv/basesystem/lib64/python3.9/site-packages/trytond/backend/postgresql/database.py", line 68, in execute
    cursor.execute(self, sql, args)
psycopg2.errors.DatatypeMismatch: operator class "varchar_pattern_ops" does not accept data type integer

```

I think there is somewhere a naming clash. It doesn’t matter but it is good to know. I also think that using a field called ‘name’ in such a model is bad practice.

---

<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: [January 4, 2023, 10:24pm UTC](https://discuss.tryton.org/t/do-not-call-a-field-name-inside-your-many2many-model/5797/4 "2023-01-04T22:24:49Z")

</div>

I understand now. Indeed the default value for `_rec_name` is `name` and `_rec_name` should point to a `Char` field if it points to a field. This was always the case but I guess you never reach a part of the code that use it with your model. But in 6.6 if the `_rec_name` field exists we create an `Similarity` index on it which of course can not be created for a column that is not a `VARCHAR` or `TEXT` in the database.  
So you can still use your `name` field as-is but you have the change the value of `_rec_name`.

Indeed we have a generic test `test_rec_name` in `test_tryton.py` which enforce this. You should run them on your custom module.

---

<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: [February 3, 2023, 10:25pm UTC](https://discuss.tryton.org/t/do-not-call-a-field-name-inside-your-many2many-model/5797/5 "2023-02-03T22:25:48Z")

</div>

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