(pk) UUID генерируются в базе данных, перезагрузка не работает, так как требуется pk. Миграция:
class CatSchema extends Schema {
up () {
this.createExtensionIfNotExists('uuid-ossp');
this.createExtensionIfNotExists('pgcrypto');
this.create('cats', (table) => {
table.uuid('id')
.primary()
.unique()
.defaultTo(this.db.raw('public.gen_random_uuid()'))
table.string('name')
table.timestamps()
})
}
как получить идентификатор в модели? контроллер:
'use strict'
const Cat=use('App/Models/Cat')
class CatController {
index(){
return Cat.all()
}
async store({request}) {
const name = request.input('name')
const cat = await Cat.create({name});
return cat.id
//there is no id here, but there is one in the database
}
}
module.exports = CatController