# Unique constraint for a selection field

**URL:** <https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251>\
**Category:** Developer\
**Created:** [January 29, 2020, 9:50am UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251 "2020-01-29T09:50:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![prak](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/prak/32/321_2.png) [@prak](https://discuss.tryton.org/u/prak)\
**Post date:** [January 29, 2020, 9:50am UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251/1 "2020-01-29T09:50:54Z")

</div>

We (@Thedevilcoder and me) are stuck at a very trivial issue. We are trying to create a selection field and getting this error (with traceback)-

```
Traceback (most recent call last):
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/protocols/dispatcher.py", line 176, in _dispatch
    result = rpc.result(meth(*c_args, **c_kwargs))
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/wizard/wizard.py", line 287, in execute
    return wizard._execute(state_name)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/wizard/wizard.py", line 318, in _execute
    result = self._execute(transition())
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/ir/module.py", line 582, in transition_upgrade
    pool.init(update=update, lang=lang)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/pool.py", line 160, in init
    lang=lang, activatedeps=activatedeps)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/modules/ __init__.py", line 423, in load_modules
    _load_modules(update)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/modules/ __init__.py", line 388, in _load_modules
    load_module_graph(graph, pool, update, lang)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/modules/ __init__.py", line 216, in load_module_graph
    cls. __register__ (module)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/model/modelsql.py", line 201, in __register__
    super(ModelSQL, cls). __register__ (module_name)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/model/model.py", line 123, in __register__
    Translation.register_fields(cls, module_name)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/ir/translation.py", line 240, in register_fields
    insert_selection(field, trans_name)
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/ir/translation.py", line 231, in insert_selection
    '', module_name, False, -1]]))
  File "/home/prakhar/workspace/tryton/tryton50/src/trytond/trytond/backend/postgresql/database.py", line 65, in execute
    cursor.execute(self, sql, args)
psycopg2.IntegrityError: duplicate key value violates unique constraint "ir_translation_translation_md5_uniq"
DETAIL: Key (name, res_id, lang, type, src_md5, module)=(gdm.workflow,state, -1, en, selection, ea4788705e6873b424c65e91c2846b19, gdm_cohort) already exists.

```

The code is very simple and we have reduced the model to only this content -

```
from trytond.model import (ModelSQL, ModelView, fields, Workflow)
from trytond.pyson import Eval

__all__ = ['GdmWorkflow',
]

class GdmWorkflow(Workflow, ModelSQL, ModelView):
    'GDM'

    __name__ = 'gdm.workflow'

    state = fields.Selection(
        [
            ('draft', 'Draft'),
            ('f_to_administer', 'Forwarded to Administer'),
            ('cancel_deo', 'Cancel'),
            ('f_to_reviewer', 'Forwarded to Reviewer'),
            ('cancel_administer', 'Cancel'),
            ('f_b_to_administer', 'Sent to administer for further administration'),
            ('finalise_form', 'Form Freezed'),
        ], 'Status')

```

We know that we are missing a minor detail here but are not able to figure out.

---

<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 29, 2020, 12:11pm UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251/2 "2020-01-29T12:11:41Z")

</div>

> [@prak](#):
>
> DETAIL: Key (name, res\_id, lang, type, src\_md5, module)=(gdm.workflow,state, -1, en, selection, ea4788705e6873b424c65e91c2846b19, gdm\_cohort) already exists.

Do you have an .po file with the translations? It seems you have defined a translation twice.

---

<div class="post-metadata">

**Author:** ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)\
**Post date:** [January 29, 2020, 12:16pm UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251/3 "2020-01-29T12:16:34Z")

</div>

> [@edbo](#):
>
> Do you have an .po file with the translations? It seems you have defined a translation twice.

Or if you updated a database from 5.0 series, you are missing some of the migration queries. [See the 5.0 to 5.2 migration page](https://discuss.tryton.org/t/migration-from-5-0-to-5-2/995).

---

<div class="post-metadata">

**Author:** ![prak](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/prak/32/321_2.png) [@prak](https://discuss.tryton.org/u/prak)\
**Post date:** [January 30, 2020, 4:46am UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251/4 "2020-01-30T04:46:20Z")

</div>

> [@edbo](#):
>
> Do you have an .po file with the translations? It seems you have defined a translation twice.

@edbo No, there is no translation file.

> [@pokoli](#):
>
> if you updated a database from 5.0 series

We are still working on 5.0. (Trytond Version is 5.0.18)

---

<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 30, 2020, 8:32am UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251/5 "2020-01-30T08:32:19Z")

</div>

Does it happen on a clean new database?  
I think you have former translation registered in your database. You may run clean translation wizards before introduce the new changes.

---

<div class="post-metadata">

**Author:** ![prak](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/prak/32/321_2.png) [@prak](https://discuss.tryton.org/u/prak)\
**Post date:** [January 30, 2020, 12:03pm UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251/6 "2020-01-30T12:03:40Z")

</div>

@ced This is happening on a clean database. I have asked all my team mates to run this code on their respective systems on a clean database. If we all face the same issue then I will share the code with community.

---

<div class="post-metadata">

**Author:** ![prak](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/prak/32/321_2.png) [@prak](https://discuss.tryton.org/u/prak)\
**Post date:** [January 31, 2020, 12:26pm UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251/7 "2020-01-31T12:26:53Z")

</div>

The issue is solved. I guess you can’t repeat the label in the selection field. So here, cancel\_deo and cancel\_admin .. both have “Cancel” as the label and this is why the duplicate translation error was coming up.

Thanks for the help !!

---

<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:** [March 1, 2020, 12:26pm UTC](https://discuss.tryton.org/t/unique-constraint-for-a-selection-field/2251/8 "2020-03-01T12:26:55Z")

</div>

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