使用关联种子数据进行KnexJS迁移 [英] KnexJS Migration With Associated Seed Data

查看:188
本文介绍了使用关联种子数据进行KnexJS迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习BookshelfJS / KnexJS(从SequelizeJS转换),我遇到了将数据导入多个表的问题,这些表是通过KnexJS中的迁移功能创建的。有4个表:

I'm, in the process of learning BookshelfJS/KnexJS (switching from SequelizeJS), and I'm running into an issue with importing data into multiple tables that were created via the migrations feature within KnexJS. There's 4 tables:


  1. 服务器

  2. operating_systems

  3. applications

  4. applications_servers

  1. servers
  2. operating_systems
  3. applications
  4. applications_servers

具有以下约束条件:


  • servers operating_system_id references operating_systems id

  • applications_servers server_id 引用服务器 id

  • applications_servers application_id 引用应用程序 id

  • servers.operating_system_id references operating_systems.id
  • applications_servers.server_id references servers.id
  • applications_servers.application_id references applications.id

当我运行<$ c $时,表格创建得很好c> knex migrate:最新--env开发服务器,当我将种子数据导入表格时,我收到错误。

The tables get created just fine when I run knex migrate:latest --env development servers, it's when I import seeded data into the tables I get an error.

最初,我将4个表的种子数据组织到目录 ./ seeds / dev 中的4个不同文件中,其中h只是 $ {table_name} .js

Originally, I organized the seed data for the 4 tables into 4 different files within the directory ./seeds/dev, which is just ${table_name}.js:


  1. operating_systems.js

  2. servers.js

  3. applications.js

  4. applications_servers.js

  1. operating_systems.js
  2. servers.js
  3. applications.js
  4. applications_servers.js

经过一些调试后,我意识到当文件 applications_servers.js 中的种子数据产生错误时,从那时起我拿出那个,其他3个运行得很好。然后,当我删除3个种子文件并将 applications_servers.js 移动到 ./ seeds / dev / 目录并执行 knex seed:run applications_servers 表填充得很好。但是,当我尝试一次导入所有4个文件的种子数据时,我收到以下错误:

After a bit of debugging, I came to the realization that the error is being generated when the seed data within the file applications_servers.js, since when I take that one out, the other 3 run just fine. Then when I remove the 3 seed files and move applications_servers.js to the ./seeds/dev/ directory and execute knex seed:run, the applications_servers table gets populated just fine. However when I try to import the seeded data of all 4 files at once, I receive the following error:

# knex seed:run
Using environment: development
Error: ER_NO_REFERENCED_ROW_2: Cannot add or update a child row: a foreign key constraint fails (`bookshelf_knex_lessons`.`applications_servers`, CONSTRAINT `applications_servers_server_id_foreign` FOREIGN KEY (`server_id`) REFERENCES `servers` (`id`) ON DELETE CASCADE)
at Query.Sequence._packetToError (/Users/me/Documents/scripts/js/node/bookshelf_knex/node_modules/mysql/lib/protocol/sequences/Sequence.js:48:14)
at Query.ErrorPacket (/Users/me/Documents/scripts/js/node/bookshelf_knex/node_modules/mysql/lib/protocol/sequences/Query.js:83:18)

并且没有一行插入任何表格。

And not a single row gets inserted, into any table.

我认为这可能与他们被导入的订单有关。 (因为它们必须按照上面列出的文件的顺序导入)。因此,我认为它们可能是以字母数字顺序引用的,我将它们重命名为:

I thought that maybe it had something to do with the order that they were being imported. (Since they have to be imported in the order that the files were listed above). So thinking that maybe they were referenced in alpha-numeric order, I renamed them to:


  1. 1-operating_systems.js

  2. 2-servers.js

  3. 3-applications.js

  4. 4-applications_servers.js

  1. 1-operating_systems.js
  2. 2-servers.js
  3. 3-applications.js
  4. 4-applications_servers.js

然而,没有任何改变,没有行插入任何表,所以为了确保,我颠倒了文件上的数字前缀序列,并再次,没有更改,没有插入单行,并返回相同的错误。

However, nothing changed, no rows were inserted into any table, so just to be sure, I reversed the sequence of the number prefixes on the files, and again, no changes, not a single row was inserted, and same error returned.

注意:用于创建表的迁移脚本以及所有种子数据是从我创建的JS文件中复制并粘贴,该文件创建相同的表并使用BookshelfJS / KnexJS导入相同的数据,但它不是使用迁移功能,而是通过节点执行时手动执行。您可以查看此文件 此处

Note: The migration script for creating the tables, as well as all of the seed data, was copy and pasted from a JS file I created which creates the same tables and imports the same data using BookshelfJS/KnexJS, but instead of using the migration features, it just does it manually when executed via node. You can view this file here

任何帮助都将不胜感激!

Any help would be appreciated!

编辑:当我将./seeds/dev中的所有种子文件合并到一个文件中时, ./seeds/dev/servers.js ,一切都进入正常。这让我觉得它可能是由异步执行的knex迁移引起的,因此ID插入了数据透视表和服务器 operating_system_id 可能尚未插入到关联的表中...如果是这种情况,有没有办法在种子文件中设置依赖项?

Edit: When I combine all of the seed files within ./seeds/dev into one file, ./seeds/dev/servers.js, everything gets imported just fine. This makes me think that it may be caused by the knex migrations being executed asynchronously, thus the ID's inserted in the pivot table and the servers.operating_system_id may not be inserted yet into the associated tables... If this is the case, is there a way to setup dependencies in the seed files?

推荐答案

Knex.js的种子功能不提供任何执行保证顺序。每个种子都应该写成可以单独执行 - 即。您的单个文件方法是正确的。

Knex.js's seed functionality does not provide any order of execution guarantees. Each seed should be written such that it can be executed in isolation - ie. your single file approach is correct.

如果您想将单个种子文件分解为子模块,那么您可以尝试以下方法:

If you want to break your individual seed files into submodules, then you might try the following:

// initial-data.js
var operatingSystems = require('./initial-data/operating-systems.js');
var servers = require('./initial-data/servers.js');
exports.seed = function(knex, Promise) {
  return operatingSystems.seed(knex, Promise)
  .then(function () {
    return servers.seed(knex, Promise);
  }).then(function() {
    // next ordered migration...
  });
}

这篇关于使用关联种子数据进行KnexJS迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆