1

I need to add a column to my table of riders, allowing us to store the name of the image that will display on that rider's card. I then need to update all of the records with the auto-generated image names.

I've done a bunch of searching, and all roads seem to lead back to this thread or this one. I've tried the code from both of these threads, swapping in my own table and column names, but I still can't get it to work.

This is the latest version of the code:

export async function up(knex, Promise) {
  return knex.transaction(trx => {
    const riders = [
        {
          name: 'Fabio Quartararo',
          card: 'rider_card_FabioQuartararo'
        },
        ...24 other riders here...
        {
          name: 'Garrett Gerloff',
          card: 'rider_card_GarrettGerloff'
        },
    ];
    return knex.schema.table('riders', (table) => table.string('card')).transacting(trx)
      .then(() =>{
        const queries = [];
        riders.forEach(rider => {
            const query = knex('riders')
                .update({
                    card: rider.card
                })
                .where('name', rider.name)
                .transacting(trx); // This makes every update be in the same transaction
            queries.push(query);
        });
    
        Promise.all(queries) // Once every query is written
          .then(() => trx.commit) // We try to execute all of them
          .catch(() => trx.rollback); // And rollback in case any of them goes wrong
      });
  });
}

When I run the migration, however, it fails with the following error:

migration file "20211202225332_update_rider_card_imgs.js" failed
migration failed with error: Cannot read properties of undefined (reading 'all')    
Error running migrations:  TypeError: Cannot read properties of undefined (reading 'all')
    at D:\Users\seona\Documents\_Blowfish\repos\MotoGP\dist\database\migrations\20211202225332_update_rider_card_imgs.js:134:25
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

So it's clearly having some sort of problem with Promise.all(), but I can't for the life of me figure out what. Searching has not turned up any useful results.

Does anyone have any ideas about how I can get this working? Thanks in advance.

1 Answers1

1

I think you might be following some older documentation and/or examples (at least that's what I was doing).

The Promise argument is no longer passed into the migration up and down functions.

So, the signature should be something like this:

 function up(knex) {
    // Use the built in Promise class
    Promise.all(<ARRAY_OF_QUERY_PROMISES>);
    ...
  }
steamedcotton
  • 401
  • 4
  • 5