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

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:

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.

Which error? Can you provide the traceback?

>>>> 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.

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.