I dig my db out of an array that looks like this (words and definitions are in many ways):
var seeds = [ { "word": "Click", "definitions": ["Computer", "Mouse", "Tasto", "Pulsante", "Selezionare"] }, { "word": "Galoppo", "definitions": ["Cavallo", "Andatura", "Trotto", "Ippica", "Passo"] }, { "word": "Raggio", "definitions": ["Sole", "Bicicletta", "Diametro", "Luce", "Laser"] }, { . . .goes on for 1089 objects
This is what I tried:
exports.seed = function (knex, Promise) { var promises = seeds.map(function (seed) { return knex('words').insert({ word: seed.word }, 'id').then(function (word_id) { var promises = seed.definitions.map(function (definition) { return knex('definitions').insert({ definition: definition }, 'id').catch(function (err) { if (err.code === 1062) return knex('definitions').select('id').where({ definition: definition }).then(function (duplicate) { return knex('definitions_words').insert({ definition_id: duplicate[0].id, word_id: word_id }); }); }).then(function (definition_id) { return knex('definitions_words').insert({ definition_id: definition_id, word_id: word_id }); }); }); return Promise.all(promises); }); }); return Promise.all(promises); };
The words are unique in my seeds, but the definitions can be repeated, so I catch a duplication error and grab the duplicate identifier to put this in the join table. This seems like normal, the join table actually ends with 1089 * 5 lines (5445), but I get an error message in cli:
Error: Cannot add or update a child row: a foreign key constraint fails (`mytable`.`definitions_words`, CONSTRAINT `definitions_words_definition_id_foreign` FOREIGN KEY (`definition_id`) REFERENCES `definitions` (`id`))