Knex迁移种子完成时出现外键错误

Dav*_*oli 5 javascript mysql node.js knex.js

我正在从一个看起来像这样的数组中播种我的数据库(单词和定义有多对多关系):

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
Run Code Online (Sandbox Code Playgroud)

这是我试过的:

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);
};
Run Code Online (Sandbox Code Playgroud)

单词在我的种子中是唯一的,但定义可能会重复,所以我抓住重复错误并获取副本的id以将其放入联结表中.它似乎工作正常,连接表实际上最终有1089*5行(5445),但我在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`))
Run Code Online (Sandbox Code Playgroud)

Ric*_*her 0

尽管我们看不到您的迁移(这是一个相当老的问题),但这些外键限制通常是您definition_idwords引用中定义的definitions.id。因此,您无法在 it 引用存在word之前创建 the definition

如果没有测试它,也没有错误检查,我想你会使用更像这样的东西:

exports.seed = function (knex, Promise) {
  var promises = seeds.map(function (seed) {
    // Check for an existing definition. More recently
    // you can use `whereNotExists` but you always need
    // an id here whatever the case
    return knex('definitions')
      .select('id')
      .where('definition', seed.definition)
      .then(function (definition_id) {
        if (definition_id.length === 1) return definition_id[0]
        return knex('definitions')
          .insert({ definition: definition })
      })
      .then(function (definition_id) {
        // Use the definition once it exists
        return knex('words')
          .insert({ word: seed.word, definition_id: definition_id })
          .then(function (word_id) {
            return { word_id: word_id, definition_id: definition_id }
          });
      })
      .then(function (join_ids) {
        // Finally, update the join table
        return knex('definitions_words')
          .insert({
            definition_id: join_ids.definition_id,
            word_id: join_ids.word_id
          })
      })

  return Promise.all(promises);
};
Run Code Online (Sandbox Code Playgroud)