how to generate a new uuid or md5 random to save on postgresql, for example:
But the 'knex.raw('uuid_generate_v4()')' does not work, I wonder how can I do to generate a random hash and save automatic when creating a new record?
and it also seems that uuid-v4 can not generate random because of migration that is static and unchanged for each request.
Sorry I'm studying node due to own and my only help is google and github, I thank you for your help.
What is the reason why
But the 'knex.raw('uuid_generate_v4()')'
Doesn't work? Does that work with postgrsql when you try to use it with plain sql wirhout knex?
Try also running your code with DEBUG=knex:* evironment variable set to get more info whats going on with your queries.
@anzileiro, you should not use uuis-v4
package in migrations, but in your models, for example before saving a new model, set its property hash
to the value of uuid()
and then save it
You need to make sure the extension is available:
knex.raw('create extension if not exists "uuid-ossp"')
then you can do
table.string('hash').defaultTo(knex.raw('uuid_generate_v4()'))
don't forget down
migration:
knex.raw('drop extension if exists "uuid-ossp"')
Edit: You can use uuid type instead of string when using pg, e.g:
await knex.schema.createTable('Users', t => {
t.uuid('id').defaultTo(knex.raw('uuid_generate_v4()'))
// ...
})
Thanks @fl0w, case closed ☺️
Thanks guys!
👍
Most helpful comment
You need to make sure the extension is available:
then you can do
don't forget
down
migration:Edit: You can use uuid type instead of string when using pg, e.g: