[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: <class 'psycopg2.errors.NotNullViolation'>: "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) <tde@openerp.com>
Signed-off-by: Ivan Elizaryev (iel) <iel@odoo.com>
This commit is contained in:
Ivan Yelizariev
2022-12-23 15:08:24 +01:00
parent 7139ae416e
commit 29fc7bdc4d
2 changed files with 5 additions and 1 deletions
+1 -1
View File
@@ -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(
@@ -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')