简体   繁体   English

如何在 AdonisJS 中运行级联种子和工厂?

[英]How to run cascade seed & factories in AdonisJS?

I'm setting up a new application I'm developing using the framework AdonisJS, I'm on the process of creating seeds and factories for my models in order to manually seed data to database easily.我正在设置一个我正在使用框架 AdonisJS 开发的新应用程序,我正在为我的模型创建种子和工厂,以便轻松地将数据手动种子到数据库。 However, my models are related to one another and I'm getting errors while trying to achieve the full seeding.但是,我的模型彼此相关,并且在尝试实现完整播种时出现错误。 I already read the Adonis documentation but it's not clear to me yet.我已经阅读了 Adonis 文档,但我还不清楚。

This is an example of the schema I'm trying to implement:这是我试图实现的模式的一个例子:

example schema示例模式

Here is the same example in plain text:这是纯文本中的相同示例:

+-----------+  +-----------------+  +-----------------+  +-----------------+
| Model A   |  | Model B         |  | Model C         |  | Model D         |
+----+------+  +----+------------+  +----+------------+  +----+------------+
| PK | id   |  | PK | id         |  | PK | id         |  | PK | id         |
+----+------+  +----+------------+  +----+------------+  +----+------------+
|    | name |  |    | name       |  |    | name       |  |    | name       |
+----+------+  +----+------------+  +----+------------+  +----+------------+
|    |      |  | FK | model_A_id |  | FK | model_B_id |  | FK | model_C_id |
+----+------+  +----+------------+  +----+------------+  +----+------------+

As you can see, the first model (Model A) has a relationship one to many with the second model (Model B) an so on.如您所见,第一个模型(模型 A)与第二个模型(模型 B)具有一对多的关系,依此类推。 I already created a migration and I already related all my models like this:我已经创建了一个迁移,并且我已经像这样关联了我的所有模型:

class ModelA extends Model {

    modelBs() {
        return this.hasMany('App/Models/ModelB')
    }
}

class ModelB extends Model {

    modelA() {
        return this.belongsTo('App/Models/ModelA')
    }

    modelCs() {
        return this.hasMany('App/Models/ModelC')
    }
}

// And so on with classes ModelC, and ModelD...

As you can see in the code above, according to the AdonisJS documentation that must be done first in order to create a relationship while seeding.正如您在上面的代码中看到的,根据 AdonisJS 文档,必须首先完成才能在播种时创建关系。 Then, I should create the factories for each model like so:然后,我应该为每个模型创建工厂,如下所示:

// File: database/factory.js

// Model A blueprint
Factory.blueprint('App/Models/ModelA', (faker) => {
  return {
    name: faker.sentence()
  }
})

// Model B blueprint
Factory.blueprint('App/Models/ModelB', (faker) => {
  return {
    name: faker.sentence()
  }
})

// And so on with Models C and D...

Finally, in my seeder file which is called ModelASeeder.js I create the relationship between all the models to relate them:最后,在我的名为 ModelASeder.js 的种子文件中,我创建了所有模型之间的关系以将它们关联起来:

//File: database/seeds/ModelASeeder.js

const Factory = use('Factory')

class ModelASeeder {
  async run () {

    // I create only one Model A.
    const modelA_var = await Factory
      .model('App/Models/ModelA')
      .create()

    // Then I makeMany of Model B (4 of them).
    const modelB_var = await Factory
      .model('App/Models/ModelB')
      .makeMany(4)

    // As the documentation says, I associate them like so:

    await modelA_var.modelBs().save(modelB_var)


    // Then I want to assign Model C with the ModelBs that I associated to ModelA before, like so (I create 8 of them):

    const modelC_var = await Factory
      .model('App/Models/ModelC')
      .makeMany(8)

    // Then I try to assign ModelB to ModelC

    await modelB_var.modelCs().save(modelC_var)

    // And so on with the model D, etc, etc....

I'm facing an issue that I cannot figure it out.我正面临一个我无法弄清楚的问题。 When I run the command adonis seed I get the following error message:当我运行命令adonis seed 时,我收到以下错误消息:

TypeError: relatedInstance.save is not a function
    at Proxy.save (/MyUser/User/my-app/node_modules/@adonisjs/lucid/src/Lucid/Relations/HasMany.js:165:28)

And at the end, it only seeds the first table (Model A table) and not the rest of them too.最后,它只播种第一个表(模型 A 表),而不是其余的表。 So I have two questions:所以我有两个问题:

First of all, why am I getting that error?首先,为什么我会收到这个错误?

Second, is this the best approach to achieve my goal of seeding my database like so?其次,这是实现我像这样播种数据库的目标的最佳方法吗?

Thank you so much for your time to answer my question, and sorry for if I made a mistake in my description, my English is not perfect.非常感谢您抽出时间回答我的问题,如果我的描述有误,我的英语并不完美,请见谅。

Try using saveMany() instead of save().尝试使用saveMany()而不是 save()。

To persist multiple instances in hasMany or belongsToMany relationships you need to use saveMany() .要在hasManybelongsToMany关系中保留多个实例,您需要使用saveMany()

Documentation: https://adonisjs.com/docs/4.0/relationships#_savemany文档: https : //adonisjs.com/docs/4.0/relationships#_savemany

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM