Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
8 Antwoorden
6195 Weergaven

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'),

}

Avatar
Annuleer
Beste antwoord

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 

Avatar
Annuleer
Auteur

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 :(

Auteur

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 :(

Beste antwoord

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

Avatar
Annuleer
Beste antwoord

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


Avatar
Annuleer
Auteur

I tried but still getting an error :(

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!'),]

Auteur Beste antwoord

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'),

}

Avatar
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
jul. 25
410
1
jun. 25
1039
0
mei 25
1157
2
apr. 25
3152
1
feb. 25
1360