title: “Modify Column” description: “Change the type or attributes of an existing column with modifycolumn, including when to use the recreate flag.”
Modify Column
The modifycolumn method allows you to change the properties of an existing column.
Usage
To modify a column, use the modifycolumn key inside an altertable definition and specify the new properties for the column.
{
"_id": "1.2.0",
"altertable": {
"users": {
"modifycolumn": {
"email": {
"type": "VARCHAR",
"length": 512
}
}
}
}
}Postgres column modifications are emitted as separate ALTER COLUMN statements. AlphaDB reads the current column type from previous versions, so you can change attributes such as null, default, unique, or generated without repeating the type.
{
"_id": "1.3.0",
"altertable": {
"users": {
"modifycolumn": {
"email": {
"null": true,
"default": "[email protected]"
}
}
}
}
}On Postgres, a modified column is set to NOT NULL unless you provide "null": true.
MySQL
On MySQL, any properties you don’t specify remain unchanged when recreate is false. If recreate is true, the column is rebuilt from the properties you provide.
Recreating a column
If you need to make a change that requires a column to be dropped and recreated (like changing a column’s fundamental type in a way that requires a full table scan), you can set the recreate flag to true. This will drop all previous properties of the column.
{
"_id": "1.3.0",
"altertable": {
"users": {
"modifycolumn": {
"email": {
"recreate": true,
"type": "TEXT",
"null": true
}
}
}
}
}In this example, the email column will be completely recreated as a TEXT column, and all its previous properties (like length and unique) will be removed.