Skip to main content

Turn a property into a mandatory property

Turn a property into a mandatory property​

To make a property mandatory, a transformation document must be provided. This is because the property must be added to the schema, and the existing data must be updated to provide a default value wherever the column was null before.

MySchema:
type: object
version: 1
properties:
field1:
type: string

The following updated schema:

MySchema:
type: object
version: 2
properties:
field1:
type: string!

The following migration document:

- target: MySchema
fromVersion: 1
toVersion: 2
properties:
field1: "field1 ?? 'default value'"
tip

Just putting "'default value'" will overwrite the entire column with the constant value. This may be fine with a previously nonexistent or unpopulated column, but this will overwrite any existing data in the column. This is why you would typically want to use the ?? (coalesce) operator.

Array properties​

On Postgres only, it is possible to turn an array from optional to mandatory:

MySchema:
type: object
version: 1
properties:
field1:
type:
array: string

The following updated schema:

MySchema:
type: object
version: 2
properties:
field1:
type:
array: string
required: true

The following migration document:

- target: MySchema
fromVersion: 1
toVersion: 2
properties:
field1: "field1 ?? ['default value']"
caution

This transform is not possible on Snowflake, as Snowflake does not support making a column with a VARIANT, OBJECT or ARRAY type mandatory.