Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to fix a error with a field unique?

By
Anabela Damas
on 4/15/13, 10:51 AM 3,600 views

Hi,

To create a list of clients and suppliers with a sequence number I made like this:

mymodule.py

...
class res_partner(osv.osv):
    _inherit = 'res.partner'
    _name = 'res.partner'

    _columns = {
        'n_client' : fields.char('Client Number', size=64, readonly=True),
        'n_supplier' : fields.char('Supplier Number', size=64, readonly=True),
    }
    _defaults = {
        'n_client': lambda obj, cr, uid, context: '/',
        'n_supplier': lambda obj, cr, uid, context: '/',        
    }
    _sql_constraints = [
        ('name_uniq_1', 'unique(n_client)', 'Number of client must be unique!'),
        ('n_supplier_uniq', 'unique(n_supplier)', 'Number of supplier must be unique!'),        
    ]

    def create(self, cr, uid, vals, context=None):
        if vals.get('n_client','/')=='/':
            vals['n_client'] = self.pool.get('ir.sequence').get(cr, uid, 'res.partner.customer') or '/'     
        if vals.get('n_supplier','/')=='/':
            vals['n_supplier'] = self.pool.get('ir.sequence').get(cr, uid, 'res.partner.supplier') or '/'
        return super(res_partner,self).create(cr, uid, vals, context=context)

    def copy(self, cr, uid, id, default=None, context=None):
        default.update({
            'n_client': self.pool.get('ir.sequence').get(cr, uid, 'res.partner.customer'),
            'n_supplier': self.pool.get('ir.sequence').get(cr, uid, 'res.partner.supplier')
        })
        return super(res_partner, self).copy(cr, uid, id, default, context)
res_partner()
...

mymoodule_sequence.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data noupdate="1">
            <record model="ir.sequence.type" id="seq_type_res_partner">
                <field name="name">number_client_sequence</field>
                <field name="code">res.partner.customer</field>
            </record>
            <record model="ir.sequence" id="seq_res_partner">
                <field name="name">number_client_sequence</field>
                <field name="code">res.partner.customer</field>
                <field name="prefix">C</field>
                <field name="padding">5</field>
            </record>
            <record model="ir.sequence.type" id="seq_type_res_supplier">
                <field name="name">number_supplier_sequence</field>
                <field name="code">res.partner.supplier</field>
            </record>
            <record model="ir.sequence" id="seq_res_supplier">
                <field name="name">number_supplier_sequence</field>
                <field name="code">res.partner.supplier</field>
                <field name="prefix">S</field>
                <field name="padding">5</field>
            </record>
    </data>
</openerp>

I get the numbering, but I have this error:

2013-04-15 12:41:07,379 4932 ERROR infinitechoice openerp.sql_db: bad query: ALTER TABLE "res_partner" ADD CONSTRAINT "res_partner_name_uniq_1" unique(n_client)
Traceback (most recent call last):
  File "/opt/openerp-7.0/openerp/sql_db.py", line 227, in execute
    res = self._obj.execute(query, params)
IntegrityError: could not create unique index "res_partner_name_uniq_1"
DETAIL:  Key (n_client)=(/) is duplicated.

2013-04-15 12:41:07,379 4932 WARNING infinitechoice openerp.osv.orm.schema: Table 'res_partner': unable to add 'unique(n_client)' constraint !
 If you want to have it, you should update the records and execute manually:
ALTER TABLE "res_partner" ADD CONSTRAINT "res_partner_name_uniq_1" unique(n_client)
2013-04-15 12:41:07,384 4932 ERROR infinitechoice openerp.sql_db: bad query: ALTER TABLE "res_partner" ADD CONSTRAINT "res_partner_n_supplier_uniq" unique(n_supplier)
Traceback (most recent call last):
  File "/opt/openerp-7.0/openerp/sql_db.py", line 227, in execute
    res = self._obj.execute(query, params)
IntegrityError: could not create unique index "res_partner_n_supplier_uniq"
DETAIL:  Key (n_supplier)=(/) is duplicated.

2013-04-15 12:41:07,384 4932 WARNING infinitechoice openerp.osv.orm.schema: Table 'res_partner': unable to add 'unique(n_supplier)' constraint !
 If you want to have it, you should update the records and execute manually:
ALTER TABLE "res_partner" ADD CONSTRAINT "res_partner_n_supplier_uniq" unique(n_supplier)

Problably I'm doing something wrong, do you know what it is ?

Thanks

1

Gustavo

--Gustavo--
950
| 2 1 4
Buenos Aires, Argentina
--Gustavo--

Python developer, big fan of sailing

Gustavo
On 4/15/13, 12:09 PM

You already have data in the res_partner table, that's why you get the database error. Try removing the data or starting your sequence from a different number than 0

So is because of _defaults ? But without having the default value '/' I can get this to work...

Versão Integral, Anabela Damas
on 4/15/13, 12:38 PM

check your data first, you already have data on your database. The error is a database error, not an OpenERP error

Gustavo
on 4/15/13, 12:39 PM

Yes I've "Your Company" and "Administrator", so I've to change the code to prevent the error... Do you how?

Versão Integral, Anabela Damas
on 4/15/13, 12:55 PM

Yes, you need to modify the create and copy functions. When you assign the value to the vals variable, you need to add a constant value (such as 10) that represents the max value of the data already stored in your database

Gustavo
on 4/15/13, 1:00 PM

Sorry I didn't understand what you mean.

Versão Integral, Anabela Damas
on 4/15/13, 2:13 PM

Thanks I alredy fix this

Versão Integral, Anabela Damas
on 4/15/13, 2:52 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 4/15/13, 10:51 AM
Seen: 3600 times
Last updated: 3/16/15, 8:10 AM