IntegrityError not handled properly
Steps to reproduce:
install cubicweb 3.26 (python3)
create a cube with
from yams.buildobjs import (EntityType, String)
class Denomination(EntityType):
name = String(unique=True, maxsize=256)
- create and run the instance
- create 2 denominations with the same name
There is an error in the front which is a copy paste from one from psycopg2. There is a stack trace in the console:
Traceback (most recent call last):
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/web/views/basecontrollers.py", line 209, in _validate_form
ctrl.publish(None)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/web/views/editcontroller.py", line 132, in publish
self._default_publish()
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/web/views/editcontroller.py", line 199, in _default_publish
self.edit_entity(formparams)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/web/views/editcontroller.py", line 291, in edit_entity
entity.eid = eid = self._insert_entity(etype, formparams['eid'], rqlquery)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/web/views/editcontroller.py", line 243, in _insert_entity
entity = self._cw.execute(rql, rqlquery.kwargs).get_entity(0, 0)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/web/request.py", line 957, in execute
rset = self.cnx.execute(*args, **kwargs)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/session.py", line 174, in check_open
return func(cnx, *args, **kwargs)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/session.py", line 719, in execute
rset = self._execute(self, rql, kwargs, build_descr)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/statsd_logger.py", line 121, in __call__
return self.callable(*args, **kw)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/querier.py", line 565, in execute
results = plan.execute()
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/querier.py", line 187, in execute
result = step.execute()
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/ssplanner.py", line 444, in execute
result = self.plan.insert_entity_defs()
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/querier.py", line 436, in insert_entity_defs
for edef in row])
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/querier.py", line 436, in <listcomp>
for edef in row])
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/repository.py", line 784, in glob_add_entity
source.add_entity(cnx, entity)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/sources/native.py", line 581, in add_entity
self.doexec(cnx, sql, attrs)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/statsd_logger.py", line 121, in __call__
return self.callable(*args, **kw)
File "/home/ngaumont/Envs/py3int/lib/python3.7/site-packages/cubicweb/server/sources/native.py", line 684, in doexec
cursor.execute(str(query), args)
psycopg2.errors.UniqueViolation: ERREUR: la valeur d'une clé dupliquée rompt la contrainte unique « key_acea95cac9de1e6fa4ab7b08c190d0d6 »
DETAIL: La clé « (cw_name)=(test) » existe déjà.
The problem is that the message « ERREUR: la valeur d'une clé dupliquée rompt la contrainte unique « key_acea95cac9de1e6fa4ab7b08c190d0d6 » does not match any regexp from server/sources/native.py (line684). There is «key_» instead of «cstr_» in the name (naming convention in pg https://stackoverflow.com/a/4108266).
Adding «mo = re.search("(?:key|unique)_[a-z0-9]{32}", arg)» doest not solve because UniqueTogetherError search inside the table cw_cwuniquetogetherconstraint .
Adding a new case and raising ViolatedConstraint does not work because the fonction check_constraint (cubicweb/server/schema2sql.py) is called at some point and it uses the fonction name_for (cubicweb/schema.py) which return 'cstr_%(s)' % hash (thus it does not find constraint key_).
Side error specific to python2, the error from psycopg2 contains non ascii character and raise another error when printing.
original ticket : https://www.cubicweb.org/ticket/17255666