diff --git a/trytond/osv/orm.py b/trytond/osv/orm.py index ce622cdad2..077674b355 100644 --- a/trytond/osv/orm.py +++ b/trytond/osv/orm.py @@ -669,53 +669,53 @@ class ORM(object): cursor.execute("DROP INDEX \"%s_%s_index\"" % \ (self._table, k)) if isinstance(field, fields.Many2One): - ref_obj = self.pool.get(field._obj) - if ref_obj: - ref = ref_obj._table + # for some fields in ir are linked to res objects + # that are not yet in the pooler + if field._obj in ('res.user', 'res.group'): + ref = field._obj.replace('.','_') else: - ref = field._obj.replace('.', '_') - if ref != 'ir_actions': - cursor.execute('SELECT confdeltype, conname ' \ - 'FROM pg_constraint as con, ' \ - 'pg_class as cl1, pg_class as cl2, ' \ - 'pg_attribute as att1, pg_attribute as att2 ' \ - 'WHERE con.conrelid = cl1.oid ' \ - 'AND cl1.relname = %s ' \ - 'AND con.confrelid = cl2.oid ' \ - 'AND cl2.relname = %s ' \ - 'AND array_lower(con.conkey, 1) = 1 ' \ - 'AND con.conkey[1] = att1.attnum ' \ - 'AND att1.attrelid = cl1.oid ' \ - 'AND att1.attname = %s ' \ - 'AND array_lower(con.confkey, 1) = 1 ' \ - 'AND con.confkey[1] = att2.attnum ' \ - 'AND att2.attrelid = cl2.oid ' \ - 'AND att2.attname = %s ' \ - 'AND con.contype = \'f\'', - (self._table, ref, k, 'id')) - res = cursor.dictfetchall() - if res: - confdeltype = { - 'RESTRICT': 'r', - 'NO ACTION': 'a', - 'CASCADE': 'c', - 'SET NULL': 'n', - 'SET DEFAULT': 'd', - } - if res[0]['confdeltype'] != \ - confdeltype.get( - field.ondelete.upper(), - 'a'): - cursor.execute('ALTER TABLE "' + \ - self._table + '" ' \ - 'DROP CONSTRAINT "' + \ - res[0]['conname'] + '"') - cursor.execute('ALTER TABLE "' + \ - self._table + '" ' \ - 'ADD FOREIGN KEY ' \ - '("' + k + '") ' \ - 'REFERENCES "' + ref + \ - '" ON DELETE ' + field.ondelete) + ref = self.pool.get(field._obj)._table + cursor.execute('SELECT confdeltype, conname ' \ + 'FROM pg_constraint as con, ' \ + 'pg_class as cl1, pg_class as cl2, ' \ + 'pg_attribute as att1, pg_attribute as att2 ' \ + 'WHERE con.conrelid = cl1.oid ' \ + 'AND cl1.relname = %s ' \ + 'AND con.confrelid = cl2.oid ' \ + 'AND cl2.relname = %s ' \ + 'AND array_lower(con.conkey, 1) = 1 ' \ + 'AND con.conkey[1] = att1.attnum ' \ + 'AND att1.attrelid = cl1.oid ' \ + 'AND att1.attname = %s ' \ + 'AND array_lower(con.confkey, 1) = 1 ' \ + 'AND con.confkey[1] = att2.attnum ' \ + 'AND att2.attrelid = cl2.oid ' \ + 'AND att2.attname = %s ' \ + 'AND con.contype = \'f\'', + (self._table, ref, k, 'id')) + res = cursor.dictfetchall() + if res: + confdeltype = { + 'RESTRICT': 'r', + 'NO ACTION': 'a', + 'CASCADE': 'c', + 'SET NULL': 'n', + 'SET DEFAULT': 'd', + } + if res[0]['confdeltype'] != \ + confdeltype.get( + field.ondelete.upper(), + 'a'): + cursor.execute('ALTER TABLE "' + \ + self._table + '" ' \ + 'DROP CONSTRAINT "' + \ + res[0]['conname'] + '"') + cursor.execute('ALTER TABLE "' + \ + self._table + '" ' \ + 'ADD FOREIGN KEY ' \ + '("' + k + '") ' \ + 'REFERENCES "' + ref + \ + '" ON DELETE ' + field.ondelete) else: # TODO add error message logger.notify_channel('init', LOG_ERROR, '')