E_MISSING_DATABASE_ROW while record has just been saved


Hi guys,

I have a strange error that only happens sometimes… let me explain:

const order = new Order()
await order.save()

return response.redirect(`myurl/${order.code}`)

On myurl a middleware is called, here its code:

try {
   var order = await Order.findBy('code', params.code)
} catch (error) {

Sometimes and sometimes only I get a:
ModelNotFoundException: E_MISSING_DATABASE_ROW: Cannot find database row for Order model
I find it really strange because the code is returned and I can see it in my request meaning it has been created.

This code is generated by a hook:

static boot() {

   * Generate a unique code for each order
  this.addHook('afterCreate', 'OrderHook.createCode')


'use strict'
const sh = require("shorthash");

const OrderHook = exports = module.exports = {}

OrderHook.createCode = async (orderInstance) => {
  const code = sh.unique(`${orderInstance.id} somestring`)
  orderInstance.code = code.toUpperCase()