Passa al contenuto
Odoo Menu
  • Accedi
  • Provalo gratis
  • App
    Finanze
    • Contabilità
    • Fatturazione
    • Note spese
    • Fogli di calcolo (BI)
    • Documenti
    • Firma
    Vendite
    • CRM
    • Vendite
    • Punto vendita Negozio
    • Punto vendita Ristorante
    • Abbonamenti
    • Noleggi
    Siti web
    • Configuratore sito web
    • E-commerce
    • Blog
    • Forum
    • Live chat
    • E-learning
    Supply chain
    • Magazzino
    • Produzione
    • PLM
    • Acquisti
    • Manutenzione
    • Qualità
    Risorse umane
    • Dipendenti
    • Assunzioni
    • Ferie
    • Valutazioni
    • Referral dipendenti
    • Parco veicoli
    Marketing
    • Social marketing
    • E-mail marketing
    • SMS marketing
    • Eventi
    • Marketing automation
    • Sondaggi
    Servizi
    • Progetti
    • Fogli ore
    • Assistenza sul campo
    • Helpdesk
    • Pianificazione
    • Appuntamenti
    Produttività
    • Comunicazioni
    • Approvazioni
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    App di terze parti Odoo Studio Piattaforma cloud Odoo
  • Settori
    Retail
    • Libreria
    • Negozio di abbigliamento
    • Negozio di arredamento
    • Alimentari
    • Ferramenta
    • Negozio di giocattoli
    Cibo e ospitalità
    • Bar e pub
    • Ristorante
    • Fast food
    • Pensione
    • Grossista di bevande
    • Hotel
    Agenzia immobiliare
    • Agenzia immobiliare
    • Studio di architettura
    • Edilizia
    • Gestione immobiliare
    • Impresa di giardinaggio
    • Associazione di proprietari immobiliari
    Consulenza
    • Società di contabilità
    • Partner Odoo
    • Agenzia di marketing
    • Studio legale
    • Selezione del personale
    • Audit e certificazione
    Produzione
    • Tessile
    • Metallo
    • Arredamenti
    • Alimentare
    • Birrificio
    • Ditta di regalistica aziendale
    Benessere e sport
    • Club sportivo
    • Negozio di ottica
    • Centro fitness
    • Centro benessere
    • Farmacia
    • Parrucchiere
    Commercio
    • Tuttofare
    • Hardware e assistenza IT
    • Ditta di installazione di pannelli solari
    • Calzolaio
    • Servizi di pulizia
    • Servizi di climatizzazione
    Altro
    • Organizzazione non profit
    • Ente per la tutela ambientale
    • Agenzia di cartellonistica pubblicitaria
    • Studio fotografico
    • Punto noleggio di biciclette
    • Rivenditore di software
    Carica tutti i settori
  • Community
    Apprendimento
    • Tutorial
    • Documentazione
    • Certificazioni 
    • Formazione
    • Blog
    • Podcast
    Potenzia la tua formazione
    • Programma educativo
    • Scale Up! Business Game
    • Visita Odoo
    Ottieni il software
    • Scarica
    • Versioni a confronto
    • Note di versione
    Collabora
    • Github
    • Forum
    • Eventi
    • Traduzioni
    • Diventa nostro partner
    • Servizi per partner
    • Registra la tua società di contabilità
    Ottieni servizi
    • Trova un partner
    • Trova un contabile
    • Incontra un esperto
    • Servizi di implementazione
    • Testimonianze dei clienti
    • Supporto
    • Aggiornamenti
    GitHub Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Richiedi una demo
  • Prezzi
  • Aiuto

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Contabilità
  • Magazzino
  • PoS
  • Progetti
  • MRP
All apps
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
Assistenza

How to fix a error with a field unique?

Iscriviti

Ricevi una notifica quando c'è un'attività per questo post

La domanda è stata contrassegnata
sequenceuniqueres_partner
1 Rispondi
13936 Visualizzazioni
Avatar
Anabela Damas

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

0
Avatar
Abbandona
Avatar
Gustavo
Risposta migliore

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

1
Avatar
Abbandona
Anabela Damas
Autore

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

Gustavo

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

Anabela Damas
Autore

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

Gustavo

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

Anabela Damas
Autore

Sorry I didn't understand what you mean.

Anabela Damas
Autore

Thanks I alredy fix this

Ti stai godendo la conversazione? Non leggere soltanto, partecipa anche tu!

Crea un account oggi per scoprire funzionalità esclusive ed entrare a far parte della nostra fantastica community!

Registrati
Post correlati Risposte Visualizzazioni Attività
how to set unique number?
sequence constraint unique
Avatar
Avatar
1
mar 15
8602
Sequence Prefix
sequence
Avatar
0
feb 23
6163
How to customize the invoice,bill and journal name sequence
sequence
Avatar
0
gen 23
95
What is the best (correct) way to create sequence ? py or xml? Risolto
sequence
Avatar
Avatar
1
ott 22
5038
Deleted a sequence
sequence
Avatar
0
apr 22
3460
Community
  • Tutorial
  • Documentazione
  • Forum
Open source
  • Scarica
  • Github
  • Runbot
  • Traduzioni
Servizi
  • Hosting Odoo.sh
  • Supporto
  • Aggiornamenti
  • Sviluppi personalizzati
  • Formazione
  • Trova un contabile
  • Trova un partner
  • Diventa nostro partner
Chi siamo
  • La nostra azienda
  • Branding
  • Contattaci
  • Lavora con noi
  • Eventi
  • Podcast
  • Blog
  • Clienti
  • Note legali • Privacy
  • Sicurezza
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo è un gestionale di applicazioni aziendali open source pensato per coprire tutte le esigenze della tua azienda: CRM, Vendite, E-commerce, Magazzino, Produzione, Fatturazione elettronica, Project Management e molto altro.

Il punto di forza di Odoo è quello di offrire un ecosistema unico di app facili da usare, intuitive e completamente integrate tra loro.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now