javascript - Knex migration seeding completes with error -
i'm seeding db array looks this, words , definitions in many many relationship
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 1089 objects
this 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); };
words unique in seeds definitions may repeat, catch duplication error , grab id of duplicate put in junction table. seems work fine, junction table in fact ends 1089*5 rows (5445), error on cli:
error: cannot add or update child row: foreign key constraint fails (`mytable`.`definitions_words`, constraint `definitions_words_definition_id_foreign` foreign key (`definition_id`) references `definitions` (`id`))
Comments
Post a Comment