<?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_Columns_to_a_Table</id>
	<title>How to add Columns to a Table - 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_Columns_to_a_Table"/>
	<link rel="alternate" type="text/html" href="https://wiki.infinite-erp.co.id/index.php?title=How_to_add_Columns_to_a_Table&amp;action=history"/>
	<updated>2026-04-06T16:58:46Z</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_Columns_to_a_Table&amp;diff=42&amp;oldid=prev</id>
		<title>Wikiadmin: Created page with &quot;== How to add columns to an existing table ==  === Objective ===  Extend the table create in the previous ''How to Create a Table''  Howto to includ...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.infinite-erp.co.id/index.php?title=How_to_add_Columns_to_a_Table&amp;diff=42&amp;oldid=prev"/>
		<updated>2018-10-14T13:22:52Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== How to add columns to an existing table ==  === Objective ===  Extend the table create in the previous &amp;#039;&amp;#039;How to Create a Table&amp;#039;&amp;#039; &lt;a href=&quot;/index.php/How_to_create_a_Table&quot; title=&quot;How to create a Table&quot;&gt; Howto&lt;/a&gt; to includ...&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 columns to an existing table ==&lt;br /&gt;
&lt;br /&gt;
=== Objective ===&lt;br /&gt;
&lt;br /&gt;
Extend the table create in the previous ''How to Create a Table'' [[How_to_create_a_Table | Howto]] to include additional fields.&lt;br /&gt;
&lt;br /&gt;
In this howto three different fields will be added to show how to configure some of the possible references:&lt;br /&gt;
&lt;br /&gt;
# ''ValidTo'', a simple date-field matching the ValidFrom date already present in this table&lt;br /&gt;
# ''Payment Schedule'', shown as a combobox to the user to be able to choose from three values defined in a list-reference.&lt;br /&gt;
## First day of the month&lt;br /&gt;
## Mid of the month&lt;br /&gt;
## Last day of the month&lt;br /&gt;
# ''Payment Category'', a link to another existing table which allows selecting among the values present in that table.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
These changes can be done in two different locations:&lt;br /&gt;
&lt;br /&gt;
# Add columns to the '''original''' module (dbprefix '''HT''')&lt;br /&gt;
# Create a '''second''' module (dbprefix '''HT2''') which adds the columns to the first module&lt;br /&gt;
&lt;br /&gt;
The first option can be chosen if the original module author wants to add more columns to his/her module. The second option is possible for anyone as the columns are added by a new module to the existing one which is not changed directly.&lt;br /&gt;
&lt;br /&gt;
The main difference between these two methods is the names which need to be chosen for the columns to comply with the modularity naming rules.&lt;br /&gt;
&lt;br /&gt;
# Adding column to same module: Any valid column name can be picked&lt;br /&gt;
# Via second module: New column name must comply to the pattern '''EM_&amp;lt;DBPREFIX&amp;gt;_''' where ''&amp;lt;DBPREFIX&amp;gt; must be the dbprefix if the new module contaning the column to be added. In this example: '''EM_HT2_'''&lt;br /&gt;
&lt;br /&gt;
For the rest of the howto we follow this second approach and will place all new elements into a new module with dbprefix ''HT2''.&lt;br /&gt;
&lt;br /&gt;
The [[How_to_add_a_field_to_a_Window_Tab | next Howto]] will continue with adding the new columns to the window defined on top of the ''ht_salary'' table.&lt;br /&gt;
&lt;br /&gt;
To show how different types of column are configure we will be adding 3 new columns:&lt;br /&gt;
&lt;br /&gt;
* ''Valid To'' a simple column of type date&lt;br /&gt;
* ''Payment Schedule'' a list-reference containing a list of values: ''Start of Month'', ''Mid of Month'', ''End of Month''&lt;br /&gt;
* ''Salary Category'' a reference pointing to the existing ''Salary Category'' table.&lt;br /&gt;
&lt;br /&gt;
=== Creating the new module ===&lt;br /&gt;
&lt;br /&gt;
This section only lists the main important elements needed for the new module. More details can be found in the [[How_To_Create_and_Package_a_Module |How to Create and Package a Module]] tutorial.&lt;br /&gt;
&lt;br /&gt;
* dbprefix: '''HT2'''&lt;br /&gt;
* Dependencies:&lt;br /&gt;
** Core as usual&lt;br /&gt;
** ''Openbravo Howtos'' module (&amp;gt;= 1.0.0) is also needed as this new module adds columns to that existing one, and so needs to ensure the first module is present.&lt;br /&gt;
&lt;br /&gt;
=== Creating the columns in the database ===&lt;br /&gt;
&lt;br /&gt;
{|border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;5&amp;quot; cellspacing=&amp;quot;0&amp;quot; &lt;br /&gt;
! Column name&lt;br /&gt;
! Type&lt;br /&gt;
! Length&lt;br /&gt;
! Note&lt;br /&gt;
|-&lt;br /&gt;
| em_ht2_validto&lt;br /&gt;
| DATE&lt;br /&gt;
| &lt;br /&gt;
| Date that this salary is valid to. &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| em_ht2_payment_schedule&lt;br /&gt;
| VARCHAR&lt;br /&gt;
| 60&lt;br /&gt;
| When the salary is paid&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| em_ht2_c_salary_category_id&lt;br /&gt;
| VARCHAR&lt;br /&gt;
| 32&lt;br /&gt;
| Link to the Salary Category&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
To create the above table within the database, use one of the following ALTER TABLE statements depending on the DB you are using: &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 COLUMN em_ht2_validto timestamp without time zone;&lt;br /&gt;
  ALTER TABLE ht_salary ADD COLUMN em_ht2_payment_schedule VARCHAR(60);&lt;br /&gt;
  ALTER TABLE ht_salary ADD COLUMN em_ht2_c_salary_category_id VARCHAR(32);&lt;br /&gt;
  ALTER TABLE ht_salary ADD CONSTRAINT &amp;quot;em_ht2_c_salary_category&amp;quot; FOREIGN KEY (em_ht2_c_salary_category_id) REFERENCES c_salary_category(c_salary_category_id);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As can be seen in the SQL a foreign key is added along with the new field linking to the c_salary_category table. This ensures that only existing categories can be selected and also that no salary category can be deleted as long as it is used in the ht_salary table.&lt;br /&gt;
&lt;br /&gt;
Taking a look at the folder structure of the module after running ''ant export.database'' shows that the new columns have been exported into a file in a 'modifiedTables' folder instead of the usual 'tables' to indicate that this module does not create the ''ht_salary'' table but instead is adding new elements to it.&lt;br /&gt;
&lt;br /&gt;
[[Image:AddColumnToTable1.png|center]]&lt;br /&gt;
&lt;br /&gt;
=== Adding &amp;amp; Configuring the columns in the Application Dictionary ===&lt;br /&gt;
&lt;br /&gt;
In this part we will add the newly added column to the list of [[Database_Model/org.openbravo.model.ad.datamodel/AD_Column | columns]] already defined for the ''ht_salary'' table and then configure those column definition to match the description giving in the objective section above.&lt;br /&gt;
&lt;br /&gt;
==== Adding the new column to the Application Dictionary ====&lt;br /&gt;
&lt;br /&gt;
This is the same process as described in the previous [[How_to_create_a_Table#Registering_the_table_within_the_Application_Dictionary | HowTo]]. &lt;br /&gt;
&lt;br /&gt;
# In the ''''Tables and Columns'''' window search for the entry of the ''ht_salary'' table.&lt;br /&gt;
# With this record selected, run the ''''Create columns from DB'''' process. As the table already contains several column only column which are not yet present in the Application Dictionary definition of that table will be added. In this HowTo this process will add our newly created 3 columns to the list. Notice that those new entries are automatically associated with the new module with prefix ''HT2'' as the process detected this via the naming of the database columns.&lt;br /&gt;
&lt;br /&gt;
==== Configuring the new columns ====&lt;br /&gt;
&lt;br /&gt;
Before starting to configure the new columns two preparatory steps need to be done.&lt;br /&gt;
&lt;br /&gt;
# Create a ''List Reference'' for the column ''Payment Schedule'', to define the 3 values which should be allowed for this list.&lt;br /&gt;
# Create ''Table'' reference for the ''em_ht2_c_salary_category_id'' column as the standard ''TableDir' reference cannot be used with ''em_'' type columns.&lt;br /&gt;
&lt;br /&gt;
When adding these new elements care should be taken to place them in the new module with prefix ''HT2''.&lt;br /&gt;
&lt;br /&gt;
The first step is to create a new ''''Reference'''' to hold the list of values for the ''Payment Schedule'' column. Important values to configure here are:&lt;br /&gt;
&lt;br /&gt;
* ''Parent Reference'' = ''''List'''' in the Reference definition itself, to define it as a List Reference.&lt;br /&gt;
* For each entry in the ''List Reference'' tab&lt;br /&gt;
** ''Search Key'' The value stored in the database field when this entry in selected by a user in the ComboBox.&lt;br /&gt;
** ''Name'' The user visible (translatable) text shown in the UI.&lt;br /&gt;
** ''Sequence'' to define the order of the entries which should be used in the UI.&lt;br /&gt;
&lt;br /&gt;
The following screenshot shows how the defined reference will look like.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:TableAddColumn_1.png|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The second step is to create a ''''Table Reference'''' do define how the new 'Salary Category'' field is linked with the ''c_salary_category' table.&lt;br /&gt;
&lt;br /&gt;
For this again a new ''''Reference'''' needs to be created. In this case the important values for it are the following:&lt;br /&gt;
&lt;br /&gt;
* ''Parent Reference'' = ''''Table''' to denote this is a Table Reference.&lt;br /&gt;
* In the ''Table Reference'' Tab:&lt;br /&gt;
** ''Table'' = ''c_salary_category' as this is the target table our new column will be pointing to.&lt;br /&gt;
** ''Key Column'' = ''c_salary_category_id'' as this is the primary key of the target table&lt;br /&gt;
** ''Display Column'' = ''Name'' to denote the field of this table which should be shown in the UI for this column.&lt;br /&gt;
&lt;br /&gt;
Again the following screenshot shows how the defined reference will look like.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:TableAddColumn_2.png|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After this two preparatory steps we can finally configure the new columns to use the reference we just created.&lt;br /&gt;
&lt;br /&gt;
In the ''''Tables and Columns'''' window we search for the entry to the ''ht_salary'' table. In the column tab we make the following changes for our new columns:&lt;br /&gt;
&lt;br /&gt;
# ''Payment Schedule'', change the '''Reference''' of this column from ''String'' to '''List''' and change the '''Reference Search Key''' to our newly created List Reference with name '''EM_Ht2_Payment_Schedule'''.&lt;br /&gt;
# ''Salary Category'', change the '''Reference''' of this column from ''TableDir'' to '''Table''' and change the '''Reference Search Key''' to the new '''ht_salary_c_salary_category'''.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The final step would be to run the '''Synchronize Terminology''' process and update the created elements to have useful names for the UI.&lt;br /&gt;
&lt;br /&gt;
However as long as issue {{IssueReference|issuenum=10886}} is unfixed this process will not map existing elements correctly for columns following the ''EM_'' naming rules.&lt;br /&gt;
&lt;br /&gt;
To work around this and reuse the existing core elements for the two columns for which such exists (''validto'', ''Salary Category'') assign the existing elements manually to those two columns.&lt;br /&gt;
&lt;br /&gt;
For this the following changes are needed in the same open window '''Tables and Columns''' still open from the last step and in the '''Columns''' tab of the ''ht_salary'' table.&lt;br /&gt;
&lt;br /&gt;
# ''ValidTo'' change the '''Application Element''' to ''ValidTo - Valid To Date''&lt;br /&gt;
# ''Salary Category'' change the '''Application Element''' to ''C_Salary_Category_ID - Salary Category''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now run the '''Synchronize Terminology''' process to set [[Database_Model/org.openbravo.model.ad.ui/AD_Element | element]] for the last column which does not yet have a matching element.&lt;br /&gt;
&lt;br /&gt;
After this the new columns should look like shown in the following screenshot:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:TableAddColumn_3.png|center]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As a final step we now update the newly create ''EM_Ht2_Payment_Schedule'' element to have a useful label for the UI.&lt;br /&gt;
&lt;br /&gt;
Navigating to the '''Application Dictionary || Element''' we search for the new element and to the following updates:&lt;br /&gt;
&lt;br /&gt;
# '''Name''' change from ''EM_Ht2_Payment_Schedule'' to '''Payment Schedule'''&lt;br /&gt;
# '''Print Name''' change from ''EM_Ht2_Payment_Schedule'' to '''Payment Schedule'''&lt;br /&gt;
&lt;br /&gt;
Those updates are needed to have useful labels in the UI for any windows defined on this table and to avoid those having internal names using the technical ''EM_''-terminology.&lt;br /&gt;
&lt;br /&gt;
=== Rebuilding the system ===&lt;br /&gt;
Finally to make the newly added columns available at runtime ''ant generate.entities'' needs to called and the changes deployed to tomcat. Those to steps can be done together by calling ''ant smartbuild''. After that Tomcat must be restarted to refresh the DAL In-Memory Model so it knows about the newly added columns.&lt;br /&gt;
&lt;br /&gt;
This concludes the How To on adding new columns to an existing table. Now might be a good time to [[How_To_Create_and_Package_a_Module#Exporting_a_Module | export]] your module.&lt;br /&gt;
&lt;br /&gt;
To add the new columns to the window defined on top of this table go ahead to the next [[How_to_add_a_field_to_a_Window_Tab | How to add a field to a Window Tab]].&lt;br /&gt;
&lt;br /&gt;
[[Category:HowTo]]&lt;/div&gt;</summary>
		<author><name>Wikiadmin</name></author>
		
	</entry>
</feed>