cubicweb/hooks/syncschema.py
changeset 11415 f87da59faea1
parent 11413 c172fa18565e
child 11416 9c2fbb872e91
--- a/cubicweb/hooks/syncschema.py	Thu Jul 07 14:10:45 2016 +0200
+++ b/cubicweb/hooks/syncschema.py	Fri Jul 08 10:17:14 2016 +0200
@@ -803,14 +803,19 @@
         elif cstrtype == 'UniqueConstraint' and oldcstr is None:
             syssource.update_rdef_unique(cnx, rdef)
             self.unique_changed = True
-        if cstrtype in ('BoundaryConstraint', 'IntervalBoundConstraint', 'StaticVocabularyConstraint'):
+        if cstrtype in ('BoundaryConstraint',
+                        'IntervalBoundConstraint',
+                        'StaticVocabularyConstraint'):
+            cstrname, check = y2sql.check_constraint(rdef, newcstr, syssource.dbhelper,
+                                                     prefix=SQL_PREFIX)
+            # oldcstr is the new constraint when the attribute is being added in the same
+            # transaction or when constraint value is updated. So we've to take care...
             if oldcstr is not None:
                 oldcstrname = 'cstr' + md5((rdef.subject.type + rdef.rtype.type + cstrtype +
-                                            (self.oldcstr.serialize() or '')).encode('ascii')).hexdigest()
-                cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s' %
-                               (SQL_PREFIX, rdef.subject.type, oldcstrname))
-            cstrname, check = y2sql.check_constraint(rdef, newcstr, syssource.dbhelper,
-                                                     prefix=SQL_PREFIX)
+                                            (self.oldcstr.serialize() or '')).encode('utf-8')).hexdigest()
+                if oldcstrname != cstrname:
+                    cnx.system_sql('ALTER TABLE %s%s DROP CONSTRAINT %s'
+                                   % (SQL_PREFIX, rdef.subject.type, oldcstrname))
             cnx.system_sql('ALTER TABLE %s%s ADD CONSTRAINT %s CHECK(%s)' %
                            (SQL_PREFIX, rdef.subject.type, cstrname, check))