как получить в модель,pk который генерируется в бд?

(pk) UUIDs are generated in the database. migration:

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()
    })
  }

how to get an id in a model? controller:

'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

Ответы (0 шт):