How To Set Default Value for an Entity Field
Setting default values for your entity fields in Amplication is straightforward using the @default()
custom Prisma attribute.
This can be helpful in scenarios where you want certain fields to have a predefined value when new records are created.
This guide will walk you through the steps to achieve this with various examples.
Steps to Set a Default Value:
- Navigate to your service's Entity Overview page.
- Click on the specific Entity field you want to set a default value for.
- On the Entity Field page, you will find a text box labeled Custom Attributes. Here, you'll enter the custom prisma
@default()
attribute. - In the Custom Attribute text field, enter the
@default()
attribute followed by the desired default value in parentheses. - After entering the default value, it will be saved automatically.
Examples
For example, to set a default string value of "DefaultText", you would enter:
@default("DefaultText")
For numeric fields, simply enter the number without quotes:
@default(5)
tip
Refer to Prisma's documentation for a full list of the values that Prisma gives you access to for the @default()
custom attribute.