简体   繁体   English

在 sequelize.js 中使用 faker 种子:基于其他列值的列值

[英]using faker seeds with sequelize.js: a column value based on the value of other column value

function deptTypes(items) { 
  return items[Math.floor(Math.random()*items.length)]; 
}
var items = ["a", "b","c","d","e","f"]; 
await queryInterface.bulkInsert('reviews', [...Array(50)].map((review) => (
  {
    user_id: users[Math.floor(Math.random()*users.length)].id, 
    title: faker.name.title(), 
    review: faker.lorem.sentences(6), 
    rating: Math.round(Math.random()*5),
    department_type: deptTypes(items), 
    department_uid: (deptTypes(items) == 'a' ) ? alist[Math.floor(Math.random()*alist.length)].id 
                      :(deptTypes(items) == 'b' ) ? blist[Math.floor(Math.random()*blist.length)].id
                        : (deptTypes(items)== 'c') ? clist[Math.floor(Math.random()*clist.length)].id 
                          : (deptTypes(items) == 'd') ? dlist[Math.floor(Math.random()*dlist.length)].id
                            : (deptTypes(items) == 'e') ? elist[Math.floor(Math.random()*elist.length)].id
                              : flist[Math.floor(Math.random()*flist.length)].id, 
    created_at: new Date(),
    updated_at: new Date()
  }
)), {});

what i'm trying is i need the same deptType item that is assigned to department_type and give department_uid a value based on department_type , but im not getting desired result ... please help我想要是我需要被分配到相同deptType项目department_type并给予department_uid基于值department_type ,但IM没有得到期望的结果...请帮助

At the top of the map function, declare a variable which will be the dept for this iteration:在 map 函数的顶部,声明一个变量,它将作为这次迭代的部门:

 await queryInterface.bulkInsert('reviews', [...Array(50)].map((review) => { const dept = deptTypes(items); return { user_id: users[Math.floor(Math.random()*users.length)].id, title: faker.name.title(), review: faker.lorem.sentences(6), rating: Math.round(Math.random()*5), department_type: dept, department_uid: (dept == 'a' ) ? alist[Math.floor(Math.random()*alist.length)].id :(dept == 'b' ) ? blist[Math.floor(Math.random()*blist.length)].id : (dept == 'c') ? clist[Math.floor(Math.random()*clist.length)].id : (dept == 'd') ? dlist[Math.floor(Math.random()*dlist.length)].id : (dept == 'e') ? elist[Math.floor(Math.random()*elist.length)].id : flist[Math.floor(Math.random()*flist.length)].id, created_at: new Date(), updated_at: new Date() }; }), {});

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

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