From fa19ca6e47b23aa0da38bbcc6b731ce64b754020 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Wed, 29 Jun 2011 18:27:46 +0200 Subject: [PATCH 1/3] [imp] modified meaning of operation 5 on o2m to delete the pointed rows if on delete cascade bzr revid: nicolas.vanhoren@openerp.com-20110629162746-dn0tosycss4a0cne --- openerp/osv/fields.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index f55e4b9c027..58a7dfec715 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -525,7 +525,13 @@ class one2many(_column): # Must use write() to recompute parent_store structure if needed obj.write(cr, user, [act[1]], {self._fields_id:id}, context=context or {}) elif act[0] == 5: - cr.execute('update '+_table+' set '+self._fields_id+'=null where '+self._fields_id+'=%s', (id,)) + reverse_rel = obj._all_columns.get(self._fields_id) + assert reverse_rel, 'Trying to unlink the content of a o2m but the pointed object does not have a m2o' + # if the pointed object has on delete cascade, just delete it + if reverse_rel.column.ondelete == "cascade": + obj.unlink(cr, user, obj.search(cr, user, [(self._fields_id,'=',id)], context), context) + else: + cr.execute('update '+_table+' set '+self._fields_id+'=null where '+self._fields_id+'=%s', (id,)) elif act[0] == 6: # Must use write() to recompute parent_store structure if needed obj.write(cr, user, act[2], {self._fields_id:id}, context=context or {}) From 4dcc76071eecf46046c4a54a177ca68e11ada6cd Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Thu, 30 Jun 2011 12:04:04 +0200 Subject: [PATCH 2/3] [imp] small bug in o2m 5 improvement bzr revid: nicolas.vanhoren@openerp.com-20110630100404-5tkdsafuf9ju4xcw --- openerp/osv/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index b2b3a309b73..d7143353086 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -529,7 +529,7 @@ class one2many(_column): assert reverse_rel, 'Trying to unlink the content of a o2m but the pointed object does not have a m2o' # if the pointed object has on delete cascade, just delete it if reverse_rel.column.ondelete == "cascade": - obj.unlink(cr, user, obj.search(cr, user, [(self._fields_id,'=',id)], context), context) + obj.unlink(cr, user, obj.search(cr, user, [(self._fields_id,'=',id)], context=context), context=context) else: cr.execute('update '+_table+' set '+self._fields_id+'=null where '+self._fields_id+'=%s', (id,)) elif act[0] == 6: From 708855467f6f4ca10a7261f7a090868a76ebaa3a Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Tue, 5 Jul 2011 10:51:06 +0200 Subject: [PATCH 3/3] [imp] changed meaning of operation 3 on o2m to behave like operation 5 bzr revid: nicolas.vanhoren@openerp.com-20110705085106-xepnebw12sbq8s9b --- openerp/osv/fields.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index d7143353086..1e926516bbb 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -520,14 +520,20 @@ class one2many(_column): elif act[0] == 2: obj.unlink(cr, user, [act[1]], context=context) elif act[0] == 3: - cr.execute('update '+_table+' set '+self._fields_id+'=null where id=%s', (act[1],)) + reverse_rel = obj._all_columns.get(self._fields_id) + assert reverse_rel, 'Trying to unlink the content of a o2m but the pointed model does not have a m2o' + # if the model has on delete cascade, just delete the row + if reverse_rel.column.ondelete == "cascade": + obj.unlink(cr, user, [act[1]], context=context) + else: + cr.execute('update '+_table+' set '+self._fields_id+'=null where id=%s', (act[1],)) elif act[0] == 4: # Must use write() to recompute parent_store structure if needed obj.write(cr, user, [act[1]], {self._fields_id:id}, context=context or {}) elif act[0] == 5: reverse_rel = obj._all_columns.get(self._fields_id) - assert reverse_rel, 'Trying to unlink the content of a o2m but the pointed object does not have a m2o' - # if the pointed object has on delete cascade, just delete it + assert reverse_rel, 'Trying to unlink the content of a o2m but the pointed model does not have a m2o' + # if the model has on delete cascade, just delete the rows if reverse_rel.column.ondelete == "cascade": obj.unlink(cr, user, obj.search(cr, user, [(self._fields_id,'=',id)], context=context), context=context) else: