postgres defaultValue isnt applied #6691
Replies: 5 comments
-
This is a situation where you need to create a migration. Payload handles Instead of trying to handle all the situations and only being able to cover some of them automatically, we let that fall into the user migration responsibility for any cases. All you need is to do:
import { sql } from 'drizzle-orm'
// in your up function:
await payload.db.drizzle.execute(sql`UPDATE bars SET foo = 'newDefault' WHERE foo IS NULL;`)
|
Beta Was this translation helpful? Give feedback.
-
Trade off is worth it for simple fields IMO. The dev experience just sucks whenever you make a minor change right now. A custom migration script can be made for more complex stuff |
Beta Was this translation helpful? Give feedback.
-
This has proven highly useful to me --- a/packages/db-postgres/src/schema/traverseFields.ts
+++ b/packages/db-postgres/src/schema/traverseFields.ts
@@ -168,14 +168,14 @@ export const traverseFields = ({
)
}
} else {
- targetTable[fieldName] = varchar(columnName)
+ targetTable[fieldName] = varchar(columnName).default(field.defaultValue)
}
break
}
case 'email':
case 'code':
case 'textarea': {
- targetTable[fieldName] = varchar(columnName)
+ targetTable[fieldName] = varchar(columnName).default(field.defaultValue)
break
}
@@ -197,14 +197,14 @@ export const traverseFields = ({
)
}
} else {
- targetTable[fieldName] = numeric(columnName)
+ targetTable[fieldName] = numeric(columnName).default(field.defaultValue)
}
break
}
case 'richText':
case 'json': {
- targetTable[fieldName] = jsonb(columnName)
+ targetTable[fieldName] = jsonb(columnName).default(field.defaultValue)
break
}
@@ -213,7 +213,7 @@ export const traverseFields = ({
mode: 'string',
precision: 3,
withTimezone: true,
- })
+ }).default(field.defaultValue)
break
} |
Beta Was this translation helpful? Give feedback.
-
PR: #6983 |
Beta Was this translation helpful? Give feedback.
-
I had to do this as a separate PR. #7368 |
Beta Was this translation helpful? Give feedback.
-
Link to reproduction
No response
Payload Version
3.0.0-beta.43
Node Version
20.12.2
Next.js Version
15.0.0-rc.0
Describe the Bug
When you change a
text
field torequired
and set thedefaultValue
the defaultValue isn't applied So you will just get data loss warnings.DATA LOSS WARNING: Possible data loss detected if schema is pushed.
Reproduction Steps
.
Adapters and Plugins
No response
Beta Was this translation helpful? Give feedback.
All reactions