7

I have created a table with name person and added a column 'phone_number' using liquibase changeset. But now I want to add a default value for it. but it did not work, so far I have tried this:

<addDefaultValue columnName="phone_number"
                 defaultValue="+923331234567"
                 tableName="person"/>

and

<changeSet author="haseeb" id="20160413123500">
    <update tableName="person">
        <column name="phone_number" type="varchar(255)" defaultValue="+923331234567"/>
    </update>
</changeSet>

and

<changeSet author="haseeb" id="20160413123501">
    <update tableName="person">
        <column name="phone_number" type="varchar(255)" value="+923331234567"/>
</update>

Can anyone point out where I did wrong and also would adding default value will add value to previously added rows?

Haseeb Mazhar Ranga
  • 555
  • 1
  • 5
  • 16

4 Answers4

6

try this

<addDefaultValue columnName="phone_number"
             defaultValue="+923331234567"
             tableName="person" columnDataType="varchar(255)"/>
LucianoMdA
  • 73
  • 1
  • 3
2

Try this:

<changeSet author="haseeb" id="20160413123501">
    <modifyDataType
        columnName="phone_number"
        newDataType="varchar(255)"
        defaultValue="+923331234567"
        tableName="person"/>
    <comment>Change default value</comment>
</changeSet>
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39
0

This is worked for expression:

- addDefaultValue:
    tableName: RT_STAGE
    columnName: CREATED_DATE
    defaultValueComputed: now()

and this for boolean:

- addDefaultValue:
    tableName: RT_STAGE
    columnName: FINAL
    defaultValueBoolean: false
RoutesMaps.com
  • 1,628
  • 1
  • 14
  • 19
0

YAML script to create table in liquibase with default value for string/varchar, integer and timestamp -

databaseChangeLog:
  - changeSet:
    id: liq_003
    author: Pravind
    changes:
      - sql:
        dbms: postgresql
        sql: DROP TABLE IF EXISTS test.fx_users CASCADE
      - createTable:
        tableName: fx_users
        remarks: "System authentication table"
        schemaName: test
        columns:             
          - column:
              name: username
              type: varchar(50)
              remarks: "user name, will be used for authentication"
              constraints:
                nullable: false
                unique: true
          - column:
              name: password
              type: varchar(50)
              remarks: "user password"
              constraints:
                nullable: false
          - column:
              name: email
              type: varchar(50)
              remarks: "User Email Id"
              constraints:
                nullable: false              
          - column:
              name: version
              type: int
              defaultValue: "0"
              remarks: "entity current version"
              constraints:
                nullable: true
          - column:
              name: created_on
              type: timestamp
              remarks: "Entity created time"
              defaultValueComputed: "now()"
              constraints:
                nullable: true
          - column:
              name: created_by
              type: varchar(50)
              remarks: "Name of the user who created this entity"
              defaultValue: "system"
              constraints:
                nullable: true
Pravind Kumar
  • 809
  • 1
  • 11
  • 10