<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.infinite-erp.co.id/index.php?action=history&amp;feed=atom&amp;title=How_to_add_a_Constraint</id>
	<title>How to add a Constraint - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.infinite-erp.co.id/index.php?action=history&amp;feed=atom&amp;title=How_to_add_a_Constraint"/>
	<link rel="alternate" type="text/html" href="https://wiki.infinite-erp.co.id/index.php?title=How_to_add_a_Constraint&amp;action=history"/>
	<updated>2026-04-06T17:00:28Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.infinite-erp.co.id/index.php?title=How_to_add_a_Constraint&amp;diff=56&amp;oldid=prev</id>
		<title>Wikiadmin: Created page with &quot;== How to add a constraint ==  The generic documentation about Constraints in Openbravo can be found  here.  === Objective ===  The HowTo on Ho...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.infinite-erp.co.id/index.php?title=How_to_add_a_Constraint&amp;diff=56&amp;oldid=prev"/>
		<updated>2018-10-14T13:49:15Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== How to add a constraint ==  The generic documentation about Constraints in Openbravo can be found &lt;a href=&quot;/index.php?title=Constraints_and_Triggers&amp;amp;action=edit&amp;amp;redlink=1&quot; class=&quot;new&quot; title=&quot;Constraints and Triggers (page does not exist)&quot;&gt; here&lt;/a&gt;.  === Objective ===  The HowTo on Ho...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== How to add a constraint ==&lt;br /&gt;
&lt;br /&gt;
The generic documentation about Constraints in Openbravo can be found [[Constraints_and_Triggers | here]].&lt;br /&gt;
&lt;br /&gt;
=== Objective ===&lt;br /&gt;
&lt;br /&gt;
The HowTo on [[How_to_add_Columns_to_a_Table | How to add Columns to a Table]] added a new ''valid to'' column to the ''ht_salary'' table.&lt;br /&gt;
&lt;br /&gt;
Logically the ''valid to'' date should always be after (or identical) to the ''valid from'' date. Enforcement of this logical restriction is possible with a  database constraint which is a ''SQL-Expression'' which check if the data is valid on all modification done to the data in this table.&lt;br /&gt;
&lt;br /&gt;
=== Modularity ===&lt;br /&gt;
&lt;br /&gt;
The changes described in this Howto are done will all be part of the module with the dbprefix '''HT2''' so will be placed in the same module which added the ''valid to'' column in the first place.&lt;br /&gt;
&lt;br /&gt;
As the constraint will be placed in the module with dbprefix '''HT2''' as just explained but the table ''ht_salary'' is defined in another module '''HT''' the constraint name must follow the usual rule and start with '''EM_HT2_'''.&lt;br /&gt;
&lt;br /&gt;
If the constraint would be added in the same module as its table, then this EM_ naming-rule would not be needed. However best practice is to let it start with the full tablename in that case to ensure its name will be unique across the database.}}&lt;br /&gt;
&lt;br /&gt;
{{(!) | Remember that in all cases the full constraint-name (like any other db-object name) is not allowed to be longer then 30 characters. }}&lt;br /&gt;
&lt;br /&gt;
=== Add constraint to database ===&lt;br /&gt;
&lt;br /&gt;
To add the constraint execute in database the following clause:&lt;br /&gt;
&lt;br /&gt;
'''PostgreSQL'''&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
alter table ht_salary add constraint em_ht2_ht_salary_date_chk check (em_ht2_validto&amp;gt;=validfrom);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Oracle'''&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
ALTER TABLE HT_SALARY ADD CONSTRAINT EM_HT2_HT_SALARY_DATES_CHK CHECK  (VALIDTO&amp;gt;=VALIDFROM) ENABLE;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{(!)|Adding an '''unique constraint''' to an existing module is considered as an API change and could affect to existing environments already populated. Before adding it evaluate the risk and consider creating a [http://wiki.openbravo.com/wiki/How_to_create_build_validations_and_module_scripts a buildvalidation] to check if the existing data complies. If it does not the buildvalidation can stop the update process and give a proper message.}}&lt;br /&gt;
&lt;br /&gt;
=== Adding a proper message ===&lt;br /&gt;
&lt;br /&gt;
Now when editing data in the ''Employee Salary || Salary'' tab and trying to use a ''Valid to'' date lying before the ''Valid from'' date we get an error message like shown below.&lt;br /&gt;
&lt;br /&gt;
However this error message isn't too useful yet for the user as it does not indicate at all why the save action was not done.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:HowToAddAConstraint1.png|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
It would be better it said something like ''&amp;quot;The Valid To date cannot be before the Valid From date&amp;quot;'' to help the user to specify the two dates correctly. This is done adding a new [[Database_Model/org.openbravo.model.ad.ui/AD_Message | Message]]. This leverage the Openbravo translation system so the message can be translated and shown in a users language.&lt;br /&gt;
&lt;br /&gt;
Details on how to create a new Message entry can be found [[Messages | here]].&lt;br /&gt;
&lt;br /&gt;
As a short summary:&lt;br /&gt;
&lt;br /&gt;
In the '''Application Dictionary || Message''' window create a new record using the following details:&lt;br /&gt;
&lt;br /&gt;
* '''Module''' ''Openbravo Howtos 2'' as this is the module containing the constraint also.&lt;br /&gt;
* '''Search key''': The search key must be exactly the same as the constraint's one, in this case ''em_ht2_ht_salary_dates_chk'' as this is the link between the constraint and the message.&lt;br /&gt;
&lt;br /&gt;
* '''Message type''': Depending on the type the UI for the message box will be different (green for success, yellow for warning...), in our case we want a red error message box, so we select ''Error''.&lt;br /&gt;
* '''Message text''': It is the user friendly message that will be displayed inside the message box. So let's enter: ''The Valid To date may not be before the Valid From date''.&lt;br /&gt;
&lt;br /&gt;
That's all now we have a message like:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:HowToAddAConstraint2.png|center]]&lt;br /&gt;
&lt;br /&gt;
=== Export database ===&lt;br /&gt;
&lt;br /&gt;
Whenever Application Dictionary or Physical database is modified, it is possible to export that information to xml files, this is the way Openbravo ERP maintains database data as part of its source code files. To do it just execute:&lt;br /&gt;
&lt;br /&gt;
  ant export.database&lt;br /&gt;
&lt;br /&gt;
For further explanations read the [[Development_Build_Tasks#Database_tasks|Development tasks document]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:HowTo]]&lt;/div&gt;</summary>
		<author><name>Wikiadmin</name></author>
		
	</entry>
</feed>