Repository navigation
[Postgres] How to make a text field UUID auto generated? #18102
Answered
by
mariovalney
mariovalney
asked this question in
Q&A
|
There's no UUID field for collections. |
Answered by
mariovalney
Sep 3, 2026
Replies: 1 comment
|
For people from the future (and Google). We can use postgresAdapter({
pool: {
connectionString: process.env.DATABASE_URI || '',
},
beforeSchemaInit: [
({ schema, adapter }) => {
for (const tableName in adapter.rawTables) {
const table = adapter.rawTables[tableName]
for (const fieldName in table.columns) {
const column = table.columns[fieldName];
const defaultValue = column?.default ?? '';
if (defaultValue === 'uuid') {
delete column.default;
column.type = 'uuid';
(column as any).defaultV7 = true;
}
}
}
return schema;
},
],
})Note: tested with Postgres 18: maybe you will need to change And just add it to collection's field: {
type: 'text',
name: 'custom_uuid',
unique: true,
required: true,
index: true,
defaultValue: 'uuid',
},Inspired here. |
0 replies
Answer selected by
mariovalney
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For people from the future (and Google).
We can use
beforeSchemaInithook onpayload.config.ts: