Liquibase Add default value
Asked Answered
G

5

9

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?

Griswold answered 13/4, 2016 at 3:59 Comment(0)
R
6

try this

<addDefaultValue columnName="phone_number"
             defaultValue="+923331234567"
             tableName="person" columnDataType="varchar(255)"/>
Rodman answered 12/7, 2016 at 13:55 Comment(0)
P
3

Try this:

<changeSet author="haseeb" id="20160413123501">
    <modifyDataType
        columnName="phone_number"
        newDataType="varchar(255)"
        defaultValue="+923331234567"
        tableName="person"/>
    <comment>Change default value</comment>
</changeSet>
Proto answered 13/4, 2016 at 4:49 Comment(0)
B
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
Barite answered 26/10, 2021 at 13:53 Comment(0)
M
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
Mcelroy answered 22/5, 2023 at 15:7 Comment(0)
S
0

Make default value when table is created

<column name="phone_number" type="varchar" defaultValue="+923331234567"> <constraints nullable="false"/> </column>

Selmore answered 27/7, 2024 at 14:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.