Raw unique exception of RDBMS API on cardinality violation on update
When on an entity type of the schema, cardinality
is defined with ?
(0 or 1), and that the constraint is violated, we would except a RQL/YAMS/CubicWeb error and RDBMS depend one. Moreover the exception should be raised on commit and not on cw_set
.
Example
Schema
class Group(EntityType):
units = SubjectRelation('GroupItem, cardinality='+?')
Automatic test
def test_group_unit_multiple_update(self):
with self.admin_access.cnx() as cnx:
group = self.create_group(cnx, production_units=(self.item1_eid,))
self.create_group(cnx, units=(self.item1_eid,))
cnx.commit()
with self.assertRaises(ValidationError) as error: # FAIL
group.cw_set(units=(self.item1_eid,self.item2_eid))
# No need of cnx.commit() to have an exception raised!
Result
-
sqlite3.IntegrityError: UNIQUE constraint failed: production_units_relation.eid_from, production_units_relation.eid_to
-
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "key_f98727f7a350360b2799dbd8b8f066a0" DETAIL: Key (eid_from, eid_to)=(9070, 9053) already exists.
Versions used
- CubicWeb 3.32.7
- RQL 0.37.0
- YAMS 0.45.3