Odoo: test unique constraint?
<p>It looks like Odoo fails at testing unique constraint.</p>

<p>I have added this constraint:</p>

<pre><code>_sql_constraints = [(
    'uniq_line',
    'unique(routing_phase_id, project_id)',
    'Phase have to be unique per Routing!')
]
</code></pre>

<p>Constraint itself works, but running unittest, I can't successfully test it.</p>

<p>I tried this:</p>

<pre><code>from psycopg2 import IntegrityError

with self.assertRaises(IntegrityError):
     self.env['project.routing.line'].create(
        self.project_routing_line_1.copy_data()[0])
</code></pre>

<p>I get this error, when running test:</p>

<pre><code>2016-05-28 13:59:16,575 19786 ERROR pas_test openerp.sql_db: bad query: INSERT INTO "project_routing_line" ("id", "routing_phase_id", "sequence", "next_routing_phase_id", "duration", "project_id", "return_routing_phase_id", "need_approve", "create_uid", "write_uid", "create_date", "write_date") VALUES(nextval('project_routing_line_id_seq'), 1, 5, NULL, 10.0, 5, 3, false, 1, 1, (now() at time zone 'UTC'), (now() at time zone 'UTC')) RETURNING id
Traceback (most recent call last):
  File "/home/oerp/openerp80/odoo/openerp/sql_db.py", line 234, in execute
    res = self._obj.execute(query, params)
IntegrityError: duplicate key value violates unique constraint "project_routing_line_uniq_line"
DETAIL:  Key (routing_phase_id, project_id)=(1, 5) already exists
</code></pre>

<p>So I don't get it, test catches IntegrityEror (or it does not?), but Odoo still tries to create record which of course fails. Am I doing something wrong here?</p>

<p><strong>Update</strong></p>

<p>I started thinking, maybe because it is SQL constraint, python does not catch such exceptions at right time (like using <code>assertRaises</code>) because it happens after all python validations? That would explain why test fails.</p>