This question has been flagged
3 Replies
17829 Views

I'm trying to copy a company and give it a new name:

company = template_user.company_id.copy(default={'name': 'New name',})

I got this error message:

ValueError: "duplicate key value violates unique constraint "res_company_name_uniq"
DETAIL:  Key (name)=(YourCompany) already exists.

YourCompany are the name of the template_users company.

 

Avatar
Discard
Best Answer

I wonder why a browse object has a copy method.

You need to call the copy mehtod of the company object.

What you might want to try is something like this:

new_company_id = self.pool.get('res_company').copy(cr, uid, YourCompany_id, default={'name': 'New name'}, context=context)

where YourCompany_id could be template_user.company_id.id

 

Regards.
 

Avatar
Discard
Author

Thank you, I have tested self.env['res.company'].copy (I dont have cr/uid) it looks like I got a copy but not any company-object nor company_id returned. def add_activity(self): template_user = self.env['res.users'].browse(literal_eval(self.env['ir.config_parameter'].get_param('auth_signup.template_user_id', 'False')),) assert template_user and template_user.exists(), 'Signup: invalid template user' for user in self: if user.company_id.id == template_user.company_id.id: company_id = self.env['res.company'].copy(template_user.company_id.id,default={ 'name': _('%s Activity') % user.name, 'currency_id': template_user.company_id.currency_id.id, 'country_id': template_user.company_id.country_id.id, 'parent_id': template_user.company_id.parent_id.id,}) _logger.warning("res.user company %s" % (company_id)) user.write({'company_ids': (6,_,[company_id,template_user.parent_id.id]),'company_id': company_id})

Best Answer

I think that might be because in the definition of the Partner model from Odoo name is an index of the database and is defined like this:

name = fields.Char(index=True)


So you should inherit that model to redefine name to have a copy=False attribute (because default is True).

class Partner(models.Model):
_inherit = 'res.partner'

name = fields.Char(index=True, copy=False)


Avatar
Discard