2

This is the error I'm getting I have receiving this odoo error when I try to duplicate a set of record. I have inherited ['mail.thread', 'ir.needaction_mixin'] in the current class. I didn't found any solution online or myself or from odoo. Still stuck here around four days.

Can anybody have an idea on this? Currently I'm using Odoo 10.

Code added is below:

@api.model
    def create(self, vals):
        res = super(StaffKPI, self).create(vals)

        fol = {}
        fol['res_model'] = 'staff.kpi'
        fol['res_id'] = res.id
        fol['partner_id'] = res.name_id.partner_id.id
        fol_id = self.env['mail.followers'].create(fol)
        self._cr.execute(
            'INSERT INTO mail_followers_mail_message_subtype_rel (mail_followers_id, mail_message_subtype_id) values (%s, %s)',
            (fol_id.id, 2))
        self._cr.execute(
            'INSERT INTO mail_followers_mail_message_subtype_rel (mail_followers_id, mail_message_subtype_id) values (%s, %s)',
            (fol_id.id, 1))

        subtypes = self.env['mail.message.subtype'].search([('res_model', '=', 'staff.kpi')]).ids
        if subtypes:
            for i in subtypes:
                self._cr.execute(
                    'INSERT INTO mail_followers_mail_message_subtype_rel (mail_followers_id, mail_message_subtype_id) values (%s, %s)',
                    (fol_id.id, i))
        old_name = res.name
        res.write({'name': ''})
        res.write({'name': old_name})
        return res
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Pablo Escobar
  • 679
  • 4
  • 20
  • Could you please add the stacktrace or that error? And the code of your new model would also help to track down the error. – CZoellner Dec 07 '18 at 10:28
  • I didn't add any new code. I just added the inheritance of mail.thread. For that I've added the below code: – Pablo Escobar Dec 07 '18 at 10:34
  • On what model did you add that inheritance? Please don't let me worm everything out of you. If you want an answer provide as much helpful information in your question as possible. – CZoellner Dec 07 '18 at 10:37

3 Answers3

3

You should use mail.threads built-in functions to add followers. You should always avoid the use of direct queries!

Following code will cover the follower and subtype adding (i didn't understand the name writes at the end):

@api.model
def create(self, vals):
    res = super(StaffKPI, self).create(vals)
    subtype_ids = self.env['mail.message.subtype'].search(
        [('res_model', '=', 'staff.kpi')]).ids
    res.message_subscribe(
        partner_ids=[res.name_id.partner_id.id],
        subtype_ids=subtype_ids)
    # other logic
    return res
CZoellner
  • 13,553
  • 3
  • 25
  • 38
1

you can use this code:

class Followers(models.Model):
   _inherit = 'mail.followers'

   @api.model
   def create(self, vals):
        if 'res_model' in vals and 'res_id' in vals and 'partner_id' in vals:
            dups = self.env['mail.followers'].search([('res_model', '=',vals.get('res_model')),
                                           ('res_id', '=', vals.get('res_id')),
                                           ('partner_id', '=', vals.get('partner_id'))])
            if len(dups):
                for p in dups:
                    p.unlink()
        return super(Followers, self).create(vals)
0

This error is because an SQL_constraint

('mail_followers_res_partner_res_model_id_uniq', 'unique(res_model,res_id,partner_id)', 'Error, a partner cannot follow twice the same object.'), ...

Located in /addons/mail/models/mail_followers.py, when you create an object the current user added automatically as follower twice in this models ['mail.followers'], so the sql_constraint will be : Unique('your_model', id_of_record_you_want_to_create, current_user) twice.

Solution : you can use option .with_context(mail_create_nosubscribe=True) and this means do not add automatically the current user as followers.

Exemple :

self.env['helpdesk.ticket'].with_context(mail_create_nosubscribe=True).create({
   'name' : 'ticket_name',
   'partner_id' : 22,
   'team_id' : 2,
}) 
      
      
khelili miliana
  • 3,730
  • 2
  • 15
  • 28