How i can make a create() method using a instance of my model in adonis /node.js?

I’m trying to make a insert using Lucid Models.

So, first i define my model in a const:

const ClasseLivroHistorico = use('App/Models/ClassBookHistoric')

This is my function that needs to create a new record:

async cadastraPrimeiroHistorico(classeId, bookUnitId, bookId){
    const primeiroClasseLivroHistorico = new ClasseLivroHistorico()
    primeiroClasseLivroHistorico.class_id = classeId;
    primeiroClasseLivroHistorico.book_unit_id = bookUnitId
    primeiroClasseLivroHistorico.book_id = bookId
    primeiroClasseLivroHistorico.status = false
    primeiroClasseLivroHistorico.created_at = new Date()
    primeiroClasseLivroHistorico.book_unit_sequence = 1
    await ClasseLivroHistorico.create(primeiroClasseLivroHistorico)

But i’m getting:

insert into “xxx” ("$attributes", “$frozen”, “$hidden”,
“$originalAttributes”, “$parent”, “$persisted”, “$relations”,
“$sideLoaded”, “$visible”, “setters”, “created_at”, “updated_at”)
values ($1, $2, DEFAULT, $3, $4, $5, $6, $7, DEFAULT, $8, $9, $10)
returning “id” - column “$attributes” of relation
“xxx” does not exist"

I’m not using .save() because this return true/false and i need to return the created record

Hey, you need to wait for the promise to resolve when you create a new object like

await new ClasseLivroHistorico()

And 2 things
Make sure you migrated the schema
With .save() you can still return the created record

1 Like