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

_sql_constraints - how to check in Odoo 7.0(oldAPI) if name already exist - still not done

By
Dr Obx
on 5/5/15, 7:47 AM 1,135 views

Hi guys,

I'm trying to check if there already is in database brand name ... but it won't work. Would you help me with?

======= c o d e =======

class product_brand(orm.Model):

_name = 'product.brand'

_sql_constraints = [('name_uniq','Check(1=1)','Brand name must be unique!'),]             # is this correct ?

_columns = {

'name': fields.char('Brand'),

'description': fields.text('Description', translate=True),

'partner_id': fields.many2one(

'res.partner',

'partner',

help='Select a partner for this brand if it exists.',

ondelete='restrict'),

'logo': fields.binary('Logo File'),

}

class product_template(orm.Model):

_inherit = 'product.template'

# or should it be placed here ?

_columns = {

'product_brand_id': fields.many2one(

'product.brand',

'Brand', help='Select a brand for this product.', ondelete='restrict'),

}

1

Solanki Shamji

--Solanki Shamji--
1293
| 5 2 6
Baroda, India
--Solanki Shamji--

Hello!

Solanki Shamji
On 5/5/15, 10:31 AM

May be this will useful to you

_sql_constraints = [

('brand_name_uniq', 'unique(field name)', 'Message you want to display')

]

And define after column definition.

Thanks.

Shamji 

Hi Solanki, so would you help me with this problem please ? Just bare in mind i need it for oldAPI. Already know how it works with new one but all my modules were built in old :( and I don't want rebuild whole system yet. Maybe in the future but I'm not ready to use new API :(

Dr Obx
on 5/9/15, 6:29 PM

Hi Solanki, so would you help me with this problem please ? Just bare in mind i need it for oldAPI. Already know how it works with new one but all my modules were built in old :( and I don't want rebuild whole system yet. Maybe in the future but I'm not ready to use new API :(

Dr Obx
on 5/9/15, 6:29 PM
0

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

Tarek Mohamed Ibrahim
On 7/6/15, 6:05 AM

I checked the word commented by Pawan "so first delete any duplicate named records" and tried to apply the constraint after deleting all the records in the table with no success, duplicates are still there. Finally I discovered that I have to DROP the table from the database and reinstall the module again so the constraint can go into the table definition. I added this reply for clarification

0

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 5/5/15, 9:52 AM

Hi Robert,

you can create a sql constraint just using unique(name) as:

_sql_constraints = [('name_uniq', 'unique(name)', 'Brand name must be unique!')]

And keep in mind that there should be no duplicate name already existing in the particular master(table), if it exists then the sql contraint will not apply on that master, so first delete any duplicate named records, and then try updating database again.

Thanks
Pawan


I tried but still getting an error :(

Dr Obx
on 5/7/15, 5:29 PM

In your Post down, "unique(brand)" ,brand must be a column in your master, but i can see that in your column list.. Please add a column " brand: fields.char(string = 'Brand'), " and then try to restart ur server by updating module in order to update the database Or if you wan to add unique constraint on product_brand_id you can use: _sql_constraints = [('brand_uniq',unique('product_brand_id'),'Brand name must be unique!'),]

Pawan
on 5/12/15, 2:14 AM
0
Dr Obx
On 5/5/15, 10:54 AM

So it should be like that:


class product_template(orm.Model):

_inherit = 'product.template'

_sql_constraints = [('brand_uniq',unique('brand'),'Brand name must be unique!'),] # is this correct ? 

_columns = {

'product_brand_id': fields.many2one(

'product.brand',

'Brand', help='Select a brand for this product.', ondelete='restrict'),

}

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

1 follower(s)

Stats

Asked: 5/5/15, 7:47 AM
Seen: 1135 times
Last updated: 7/6/15, 6:05 AM