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

Try this instead:

  this.addHook('beforeCreate', 'OrderHook.beforeCode')

Hey @begueradj,
Thanks for your reply. I can’t do that because my unique code is based on orderInstance.id which is not set before inserting a new line in my db.