Lucid ORM breaks after inserting a few seed records


Adonis seed throws this error after inserting a few records.

This error is also throw:

  WARNING: Adonis has detected an unhandled promise rejection, which may
  cause undesired behavior in production.
  To stop this warning, use catch() on promises or wrap await
  calls inside try/catch.
Error: aborted
    at PendingOperation.abort (/mnt/c/Projetos/ideciclo/backend/node_modules/tarn/lib/PendingOperation.js:25:17)
    at (/mnt/c/Projetos/ideciclo/backend/node_modules/tarn/lib/Pool.js:190:23)
    at (<anonymous>)
    at Promise.all.then.then (/mnt/c/Projetos/ideciclo/backend/node_modules/tarn/lib/Pool.js:189:34)

Here is my seed code:

const Database = use('Database')
const uuidv4 = require('uuid/v4');
const reviewData = require('./review.json');

class ReviewSeeder {
  async run() {
    await Database.table('reviews').del() (review) => {
      await Database.table('reviews').insert([{
        id: uuidv4(),
        structure_id: Database.table('structures').select('id').where({
          'street': review.street
        reviewed_at: review.reviewed_at,
        reviewer_id: Database.table('reviewers').select('reviewerId').where({
          'name': review.reviewer
        average_rating: review.average_rating,
        adequacy_rating: review.adequacy_rating,
        protection_rating: review.protection_rating,
        speed_control_rating: review.speed_control_rating,
        hor_cross_sign_rating: review.hor_cross_sign_rating,
        avg_structure_width: review.avg_structure_width,
        hor_sign_rating: review.hor_sign_rating,
        ver_sign_rating: review.ver_sign_rating,
        ver_cross_sign_rating: review.ver_cross_sign_rating,
        pattern_paint_rating: review.pattern_paint_rating,
        hor_sign_condition_rating: review.hor_sign_condition_rating,
        risk_rating: review.risk_rating,
        sinuosity_rating: review.sinuosity_rating,
        bidirectionality_rating: review.bidirectionality_rating,
        type_pavement_rating: review.type_pavement_rating,
        pavement_condition_rating: review.pavement_condition_rating,
        obstacles_rating: review.obstacles_rating, 
        safety_rating: review.safety_rating,
        comfort_rating: review.comfort_rating,
        created_at: new Date(),
        updated_at: new Date(),

module.exports = ReviewSeeder


Hello iacapuca
You have to also await when you select a record from database. So in your case you should have something like this:

structure_id: await Database.table('structures').select('id').where({ 'street': review.street }), 
reviewed_at: review.reviewed_at, 
reviewer_id: await Database.table('reviewers').select('reviewerId').where({ 'name': review.reviewer }),


Hey! :wave:

Following the answer of @melokki, I would also recommend you to use an array of Database.insert() and then do await Promise.all() to optimise the execution time of your script.


Tried that, but I keep getting the same error.


What do you mean by an array of Database.insert(), would you mind giving an example?


To be honest, I dont know why, but a --keep-alive just did the job, I was able to fully seed data into my db, however, I still dont understand why I had this error.


What do you mean by an array of Database.insert() , would you mind giving an example?

Instead of the you could build up an array of DB insert queries, and then await Promise.all()

So for example:

const inserts = [];
for (const review in reviewData) {
    // ...

await Promise.all(inserts);

Honestly, I don’t know that that is really much of an improvement, and I find the .map() to be perfectly readable, etc.

Why you were getting the unhandled promise rejection is that you are assigning values in your insert query to the result of a select query, but you’re not awaiting, so those are promises, instead of actual values.

Instead of this:

        structure_id: Database.table('structures').select('id').where({
          'street': review.street

You want this:

        structure_id: await Database.table('structures').select('id').where({
          'street': review.street


@willvincent You are right about using await before those selects, but I still have issues with this solution.
After using the awaits as suggested, I got this error:

(node:3500) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 95)
(node:3500) UnhandledPromiseRejectionWarning: error: invalid input syntax for type uuid: "{"{\"reviewerId\":\"4231c931-fdc5-4eff-9900-84924b218379\"}"}" 

And by the way, if I remove the await and do a adonis serve --keep-alive it does works just fine.