From 29fc7bdc4de5e813d0ebfed34b8a4d6615ccb27f Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Mon, 19 Dec 2022 12:49:30 +0000 Subject: [PATCH] [FIX] mail: avoid copying notified_partner_ids field The field `notified_partner_ids` has type Many2many with relation table `mail_message_res_partner_needaction_rel` [1] which is a regular Odoo model `mail.notification` with some extra fields. One of those fields is required: `notification_type`. On copying `mail.message` record, ORM copies Many2many fields directly without using `default_get` method for `mail.notification` model. Particularly, `notification_type` get null value and we get an error. STEPS 1. Create Server Action for `mail.message` model: ```py for message in (records or record): message.copy({ "subject": message.subject + "(SA copied)", }) ``` 2. Add contextual action 3. Open `mail.message` which has non-empty value on `notified_partner_ids` (*Partners with Need Action*) 4. Run the Server action via Action menu. PROBLEM ``` ValueError: : "null value in column "notification_type" of relation "mail_notification" violates not-null constraint DETAIL: Failing row contains (11, 210, null, 3, null, null, null, null, null, null, null, null, null). ``` SOLUTION Fix it by adding `copy=False`, because we don't want to copy those values anyway (confirmed by TDE). [1]: The relation table is renamed to `mail_notificaiton` in v15+ opw-3069556 closes odoo/odoo#108599 X-original-commit: d68b0f7d6b8f973ddaafdd7526d4f753a1b6abd2 Signed-off-by: Thibault Delavallee (tde) Signed-off-by: Ivan Elizaryev (iel) --- addons/mail/models/mail_message.py | 2 +- addons/test_mail/tests/test_message_post.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index e46d2663a45..1ca454b6897 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -135,7 +135,7 @@ class Message(models.Model): # mainly usefull for testing notified_partner_ids = fields.Many2many( 'res.partner', 'mail_notification', string='Partners with Need Action', - context={'active_test': False}, depends=['notification_ids']) + context={'active_test': False}, depends=['notification_ids'], copy=False) needaction = fields.Boolean( 'Need Action', compute='_compute_needaction', search='_search_needaction') has_error = fields.Boolean( diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index c43089c7f85..49472beec66 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -294,6 +294,10 @@ class TestMessageNotify(TestMessagePostCommon): ) self.assertNotIn(new_notification, self.test_record.message_ids) + # notified_partner_ids should be empty after copying the message + copy = new_notification.copy() + self.assertFalse(copy.notified_partner_ids) + admin_mails = [mail for mail in self._mails if self.partner_admin.name in mail.get('email_to')[0]] self.assertEqual(len(admin_mails), 1, 'There should be exactly one email sent to admin') admin_mail_body = admin_mails[0].get('body')