SlideShare una empresa de Scribd logo
1 de 16
How to Use Constraints and
SQL Constraints in Odoo 15
www.cybrosys.com
INTRODUCTION
 Odoo helps you to set constraints to variants which we can perform using python and
model constraints. In odoo python constraints are specified along with methods.
 This Slide will provide an insight on python and model constraints in Odoo 15.
 In python, constraints are defined along with a method decorated with constraints().
Odoo provides two ways to set up automatically verified invariants
 Python constraints
 SQL constraints
 A Python constraint is defined as a method decorated with @api.constrains,
and invoked on a recordset.
 The decorator specifies which fields are involved in the constraint, so that the
constraint is automatically evaluated when one of them is modified.
 The method is expected to raise an exception if its invariant is not satisfied:
Python Constraints
The field for which constraint is to be applied is specified along with the decorator constraints().
Can provide multiple field values as arguments in the function. Therefore, the function
gets invoked each time the value in the field gets modified/changed
There are certain limitations for using constraints()
● constrains() are not supported along with related fields. They can only be applied to simple
fields. For example related fields like partner_id.phone.
● constrains() can only be applied to fields that are included in the create and write call
because if the field is which is not contained in the view will not trigger a call to python
function.
 SQL constraints are defined through the model using _sql_constraints.
 The latter is assigned to a list of triples of strings (name, sql_definition, message),
where name is a valid SQL constraint name, sql_definition is a table_constraint_
expression, and message is the error message.
SQL Constraints
The code is used to prevent the addition of records with the same email. Because it is
irrelevant to add the same email to blacklist twice. So for situations like this where you need
to apply constraints of this type the _sql_constraints comes into action.
 name - Name for the sql constraint(eg:'unique_email')
 definition - Constraint to be applied on the table.(eg:unique (email))
 message - Validation message to be displayed. (eg: Email address
already exists!)
 Check constraint: A check constraint is defined using the CHECK keyword. Furthermore, as
the name suggests it is used to check whether it satisfies a certain condition.
 Not-Null Constraints: NOT NULL keyword is used to specify not null constraints, which
means that certain values or fields cannot be NULL.
 Unique Constraint:It is used along with a ‘unique’ keyword.The unique constraint helps
you to check whether a specified column in rows satisfies the unique constraint.
For More
Details
Check our company website for related blogs and Odoo
book.
Odoo Book V15
Check our YouTube channel for functional and technical
videos in Odoo.
How to Use Constraint and SQL Constraint? | Model
Constraints & SQL Constraints in Odoo 15
Thank You

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

QWeb Report in odoo
QWeb Report in odooQWeb Report in odoo
QWeb Report in odoo
 
Tools for Solving Performance Issues
Tools for Solving Performance IssuesTools for Solving Performance Issues
Tools for Solving Performance Issues
 
Improving the performance of Odoo deployments
Improving the performance of Odoo deploymentsImproving the performance of Odoo deployments
Improving the performance of Odoo deployments
 
What is Computed Fields and @api Depends in Odoo 15
What is Computed Fields and @api Depends in Odoo 15What is Computed Fields and @api Depends in Odoo 15
What is Computed Fields and @api Depends in Odoo 15
 
Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)Odoo (Build module, Security, ORM)
Odoo (Build module, Security, ORM)
 
Creating Menu and Actions in Odoo 15
Creating Menu and Actions in Odoo 15Creating Menu and Actions in Odoo 15
Creating Menu and Actions in Odoo 15
 
Create Data Files and Load Data in Odoo 15
Create Data Files and Load Data in Odoo 15Create Data Files and Load Data in Odoo 15
Create Data Files and Load Data in Odoo 15
 
Actions and menus in odoo 16
Actions and menus in odoo 16Actions and menus in odoo 16
Actions and menus in odoo 16
 
How to Build Custom Module in Odoo 15
How to Build Custom Module in Odoo 15How to Build Custom Module in Odoo 15
How to Build Custom Module in Odoo 15
 
Odoo - From v7 to v8: the new api
Odoo - From v7 to v8: the new apiOdoo - From v7 to v8: the new api
Odoo - From v7 to v8: the new api
 
Odoo - Smart buttons
Odoo - Smart buttonsOdoo - Smart buttons
Odoo - Smart buttons
 
How to Define Many2one Field in Odoo 15
How to Define Many2one Field in Odoo 15How to Define Many2one Field in Odoo 15
How to Define Many2one Field in Odoo 15
 
Learn How to Manage Storage Locations Odoo 15
Learn How to Manage Storage Locations Odoo 15Learn How to Manage Storage Locations Odoo 15
Learn How to Manage Storage Locations Odoo 15
 
Odoo website product stock management
Odoo website product stock managementOdoo website product stock management
Odoo website product stock management
 
How to Make Sequence Number in Odoo 16
How to Make Sequence Number in Odoo 16How to Make Sequence Number in Odoo 16
How to Make Sequence Number in Odoo 16
 
Name Search() Function in Odoo 16
Name Search() Function in Odoo 16Name Search() Function in Odoo 16
Name Search() Function in Odoo 16
 
How to use push and procurement rules in odoo
How to use push and procurement rules in odooHow to use push and procurement rules in odoo
How to use push and procurement rules in odoo
 
View Inheritance in Odoo 16
View Inheritance in Odoo 16View Inheritance in Odoo 16
View Inheritance in Odoo 16
 
scaffold method odoo 16
scaffold method odoo 16scaffold method odoo 16
scaffold method odoo 16
 
How to show warning _ error messages in Odoo 16
How to show warning _ error messages in Odoo 16 How to show warning _ error messages in Odoo 16
How to show warning _ error messages in Odoo 16
 

Similar a How to Use Constraint and SQL Constraint in Odoo 15

Relational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptxRelational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptx
Koteswari Kasireddy
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
karymadelaneyrenne19
 

Similar a How to Use Constraint and SQL Constraint in Odoo 15 (20)

Python Constraints & Model Constraints in Odoo 15
Python Constraints & Model Constraints in Odoo 15Python Constraints & Model Constraints in Odoo 15
Python Constraints & Model Constraints in Odoo 15
 
Model Constraints - SQL Constraints Odoo 16
Model Constraints - SQL Constraints Odoo 16Model Constraints - SQL Constraints Odoo 16
Model Constraints - SQL Constraints Odoo 16
 
Intro To Unit and integration Testing
Intro To Unit and integration TestingIntro To Unit and integration Testing
Intro To Unit and integration Testing
 
Chapter8 pl sql
Chapter8 pl sqlChapter8 pl sql
Chapter8 pl sql
 
Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answers
 
Bn1017 a demo rdbms
Bn1017 a demo  rdbmsBn1017 a demo  rdbms
Bn1017 a demo rdbms
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
 
Relational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptxRelational Model and Relational Algebra.pptx
Relational Model and Relational Algebra.pptx
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
Java 17
Java 17Java 17
Java 17
 
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISPMCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
MCS,BCS-7(A,B) Visual programming Syllabus for Final exams @ ISP
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
 
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
 
The Little Wonders of C# 6
The Little Wonders of C# 6The Little Wonders of C# 6
The Little Wonders of C# 6
 
Access tips access and sql part 2 putting vba and sql together
Access tips  access and sql part 2  putting vba and sql togetherAccess tips  access and sql part 2  putting vba and sql together
Access tips access and sql part 2 putting vba and sql together
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
MongoDB.pdf
MongoDB.pdfMongoDB.pdf
MongoDB.pdf
 
Model Constraints In Odoo 16
Model Constraints In Odoo 16Model Constraints In Odoo 16
Model Constraints In Odoo 16
 

Más de Celine George

Más de Celine George (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Make Down Payments in Odoo 17 Sales App
How to Make Down Payments in Odoo 17 Sales AppHow to Make Down Payments in Odoo 17 Sales App
How to Make Down Payments in Odoo 17 Sales App
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
How to Manage Engineering to Order in Odoo 17
How to Manage Engineering to Order in Odoo 17How to Manage Engineering to Order in Odoo 17
How to Manage Engineering to Order in Odoo 17
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 

Último

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

How to Use Constraint and SQL Constraint in Odoo 15

  • 1. How to Use Constraints and SQL Constraints in Odoo 15 www.cybrosys.com
  • 2. INTRODUCTION  Odoo helps you to set constraints to variants which we can perform using python and model constraints. In odoo python constraints are specified along with methods.  This Slide will provide an insight on python and model constraints in Odoo 15.  In python, constraints are defined along with a method decorated with constraints().
  • 3. Odoo provides two ways to set up automatically verified invariants  Python constraints  SQL constraints
  • 4.  A Python constraint is defined as a method decorated with @api.constrains, and invoked on a recordset.  The decorator specifies which fields are involved in the constraint, so that the constraint is automatically evaluated when one of them is modified.  The method is expected to raise an exception if its invariant is not satisfied: Python Constraints
  • 5. The field for which constraint is to be applied is specified along with the decorator constraints().
  • 6. Can provide multiple field values as arguments in the function. Therefore, the function gets invoked each time the value in the field gets modified/changed
  • 7. There are certain limitations for using constraints() ● constrains() are not supported along with related fields. They can only be applied to simple fields. For example related fields like partner_id.phone. ● constrains() can only be applied to fields that are included in the create and write call because if the field is which is not contained in the view will not trigger a call to python function.
  • 8.  SQL constraints are defined through the model using _sql_constraints.  The latter is assigned to a list of triples of strings (name, sql_definition, message), where name is a valid SQL constraint name, sql_definition is a table_constraint_ expression, and message is the error message. SQL Constraints
  • 9. The code is used to prevent the addition of records with the same email. Because it is irrelevant to add the same email to blacklist twice. So for situations like this where you need to apply constraints of this type the _sql_constraints comes into action.
  • 10.  name - Name for the sql constraint(eg:'unique_email')  definition - Constraint to be applied on the table.(eg:unique (email))  message - Validation message to be displayed. (eg: Email address already exists!)
  • 11.  Check constraint: A check constraint is defined using the CHECK keyword. Furthermore, as the name suggests it is used to check whether it satisfies a certain condition.
  • 12.  Not-Null Constraints: NOT NULL keyword is used to specify not null constraints, which means that certain values or fields cannot be NULL.
  • 13.  Unique Constraint:It is used along with a ‘unique’ keyword.The unique constraint helps you to check whether a specified column in rows satisfies the unique constraint.
  • 14. For More Details Check our company website for related blogs and Odoo book. Odoo Book V15 Check our YouTube channel for functional and technical videos in Odoo. How to Use Constraint and SQL Constraint? | Model Constraints & SQL Constraints in Odoo 15
  • 15.