0
I am a beginner with the Adonisjs framework and need to use it in an existing table with data. Researching I found that I should create a Model for the table, but it didn’t work and I couldn’t find a way to work.
I have a similar scenario to below:
A table called empresas_clientes
in a Postgres bank with the following structure:
create table empresas_clientes(
codigo serial not null primary key,
razao varchar(50) not null
);
And I created a model called Cliente
with the command adonis make:model cliente
with the following structure:
'use strict'
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')
class Cliente extends Model {
static get table() {
return 'empresas_clientes'
}
static get primaryKey() {
return 'codigo'
}
static get createdAtColumn() {
return null
}
static get updatedAtColumn() {
return null
}
}
module.exports = Cliente
I also have the controller:
'use strict'
const Cliente = require('../../Models/Cliente')
class ClienteController {
async index({ request }) {
const cliente = await Cliente.first()
return cliente
}
}
module.exports = ClienteController
And the following route:
Route.get('/clientes', 'ClienteController.index')
But when access /clientes
I get an error message
I could not solve this problem to and make the route return the JSON of the clients, I believe it is some problem due to being using an existing table.
What version of Adonisjs?
– Valdeir Psr
It is version 4.1.0
– Gabriel