Skip to Content
Menu
This question has been flagged
1 Reply
1380 Views

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
Discard
Best Answer

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
Discard
Author

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

Author

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

Related Posts Replies Views Activity
2
Sep 24
1647
1
Jul 23
3415
2
Oct 23
2612
1
Aug 23
3627
1
Sep 23
2492