İçereği Atla
Menü
Bu soru işaretlendi
1 Cevapla
1823 Görünümler

i have created a python file setups.py to create all setup related models in single file. every Model contains a " name " field and i want to be sure that these names in Donation Category, Donation Types etc. should be unique and i created same constraints with different names but the field name is same in all constraints because the setups Models have same name, created constraint like below:

sql_constraints = [
('donation_type_name_unique',
'UNIQUE(name)',
"Donation Type Name must be unique"),
]

but it is throwing error... when remove one-by-one all except first it is working fine. is there any idea how i can have constraints for all setup models in singly python file? please help.

regards


Avatar
Vazgeç
En İyi Yanıt

the issue you're facing is likely due to the fact that all your setup models have the same field name (name) and therefore the same database column name in the underlying database table. This is causing conflicts when creating the SQL constraints for uniqueness.
To ensure that the constraints are unique for each setup model, you can include the model name in the constraint name and error message. This way, each constraint will have a unique name, avoiding conflicts. Here's how you could modify your code:

try this way:

class DonationCategory(models.Model):

_name = 'your_module.donation.category'

_description = 'Donation Category'


name = fields.Char(string='Name', required=True)


_sql_constraints = [

('donation_category_name_unique',

'UNIQUE(name)',

"Donation Category Name must be unique"),

]


class DonationType(models.Model):

_name = 'your_module.donation.type'

_description = 'Donation Type'


name = fields.Char(string='Name', required=True)


_sql_constraints = [

('donation_type_name_unique',

'UNIQUE(name)',

"Donation Type Name must be unique"),

]

Avatar
Vazgeç
Üretici

thank you but don't know why it is not working, modified setups.py file, added constraint for another model, save it, restart odoo server, upgraded app, when create new record with same name as existing one (copy & past), saved new record, it is saving without any error.... is there anything else i have to do or i did anything wrong? please help.

regards

Üretici

please note, when adding module name with model name it is not starting odoo server.

İlgili Gönderiler Cevaplar Görünümler Aktivite
2
Eyl 24
2049
1
Tem 23
4085
2
Eki 23
3136
1
Ağu 23
3983
1
Eyl 23
3077