Just want to share simple tips how to using UUID in AdonisJS v5. This will help you to define a migration file when using uuid data type and give the default value for it. So, you will no need to use @beforeCreate()
on model file.
Before creating the column, you need to create extension of uuid-ossp
first by using this query:
this.db.rawQuery('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').knexQuery
Next, define the uuid column and give default value:
table.uuid('id', { primaryKey: true }).defaultTo(this.db.rawQuery('uuid_generate_v4()').knexQuery)
Top comments (0)