From 88baca367927edfd8ef45db592eeef3a7a585e11 Mon Sep 17 00:00:00 2001 From: ced <> Date: Thu, 12 Apr 2007 08:22:27 +0000 Subject: [PATCH] KERNEL: improve _sql_contsraints, now insert if doesn't exist bzr revid: ced-4b8e508445d365774aff44530f2b970ea27810c5 --- bin/osv/orm.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/bin/osv/orm.py b/bin/osv/orm.py index f02b886e90c..fb8aae39fe6 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -441,11 +441,16 @@ class orm(object): cr.execute("SELECT relname FROM pg_class WHERE relkind in ('r','v') AND relname='%s'" % self._table) create = not bool(cr.fetchone()) - if create: - for (key,con,_) in self._sql_constraints: - cr.execute('alter table %s add constraint %s_%s %s' % (self._table,self._table,key, con,)) - cr.commit() + for (key,con,_) in self._sql_constraints: + cr.execute("SELECT conname FROM pg_constraint where conname='%s_%s'" % (self._table, key)) + if not cr.dictfetchall(): + try: + cr.execute('alter table %s add constraint %s_%s %s' % (self._table,self._table,key, con,)) + cr.commit() + except: + logger.notifyChannel('init', netsvc.LOG_WARNING, 'unable to add \'%s\' constraint on table %s !\n If you want to have it, you should update the records and execute manually:\nALTER table %s ADD CONSTRAINT %s_%s %s' % (con, self._table, self._table,self._table,key, con,)) + if create: if hasattr(self,"_sql"): for line in self._sql.split(';'): line2 = line.replace('\n','').strip()