Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

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

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

How to fix a error with a field unique?

Naroči se

Get notified when there's activity on this post

This question has been flagged
sequenceuniqueres_partner
1 Odgovori
13896 Prikazi
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
Opusti
Avatar
Gustavo
Best Answer

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
Opusti
Anabela Damas
Avtor

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
Avtor

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
Avtor

Sorry I didn't understand what you mean.

Anabela Damas
Avtor

Thanks I alredy fix this

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Prijavi
Related Posts Odgovori Prikazi Aktivnost
how to set unique number?
sequence constraint unique
Avatar
Avatar
1
mar. 15
8541
Sequence Prefix
sequence
Avatar
0
feb. 23
6108
How to customize the invoice,bill and journal name sequence
sequence
Avatar
0
jan. 23
95
What is the best (correct) way to create sequence ? py or xml? Solved
sequence
Avatar
Avatar
1
okt. 22
4993
Deleted a sequence
sequence
Avatar
0
apr. 22
3423
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة 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 is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

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