diff --git a/addons/mail/models/mail_notification.py b/addons/mail/models/mail_notification.py index 1ee834b21a9..fcd5834c56d 100644 --- a/addons/mail/models/mail_notification.py +++ b/addons/mail/models/mail_notification.py @@ -44,7 +44,7 @@ class MailNotification(models.Model): failure_reason = fields.Text('Failure reason', copy=False) _sql_constraints = [ - # email notification;: partner is required + # email notification: partner is required ('notification_partner_required', "CHECK(notification_type NOT IN ('email', 'inbox') OR res_partner_id IS NOT NULL)", 'Customer is required for inbox / email notification'), @@ -59,6 +59,11 @@ class MailNotification(models.Model): CREATE INDEX IF NOT EXISTS mail_notification_res_partner_id_is_read_notification_status_mail_message_id ON mail_notification (res_partner_id, is_read, notification_status, mail_message_id) """) + self.env.cr.execute( + """CREATE UNIQUE INDEX IF NOT EXISTS unique_mail_message_id_res_partner_id_if_set + ON %s (mail_message_id, res_partner_id) + WHERE res_partner_id IS NOT NULL""" % self._table + ) @api.model_create_multi def create(self, vals_list): diff --git a/addons/test_mail/tests/test_message_post.py b/addons/test_mail/tests/test_message_post.py index 5552c0af276..c95502b5ef5 100644 --- a/addons/test_mail/tests/test_message_post.py +++ b/addons/test_mail/tests/test_message_post.py @@ -53,6 +53,7 @@ class TestMessagePost(TestMailCommon, TestRecipients): for email_xmlid in ['mail.message_notification_email', 'mail.mail_notification_light', 'mail.mail_notification_paynow']: + test_message.sudo().notification_ids.unlink() # otherwise partner/message constraint fails test_message.write({'email_layout_xmlid': email_xmlid}) with self.mock_mail_gateway(): test_record._notify_thread_by_email(