Is there any way I can put the ID attribute incremental in changeset?

Is there any way I can put the ID attribute incremental in changeset
like

Hi @aynal,

We can make a column attribute (in your case ID) incremental by creating a sequence (supported in XML schema format for liquibase) for it.

Consider you want to create a table “Employee” and want it to have 2 columns as ID and Name. Below XML changeset will create the table Employee with these columns where in column named ID will be autoincremented on every insertion to table also the startValue specifies the initial value you want for the column to start with (in our case we can keep this as 1)

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
				   http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"> 
<changeSet author="rakhi" id="create_table_employee" dbms="${dbType}">
	<createSequence sequenceName="emp_id_sequence" incrementBy="1" startValue="1" schemaName="dbSchema"/>
	<createTable tableName="Employee" schemaName="dbSchema">
		 <column name="ID" type="INTEGER" defaultValueComputed="nextval('dbSchema.emp_id_sequence')">
			 <constraints nullable="false" primaryKey="true" primaryKeyName="emp_id_pkey"/>
		</column>
		<column name="Name" type="VARCHAR(100)">
			 <constraints nullable="false"/>
		 </column>
	 </createTable>
</changeSet>
</databaseChangeLog>

I have tried this and seems to be working for me. Please make sure to replace the correct schema name in the above changeset before giving it a try.

Let me know if this helps!
Thanks!

1 Like