将 faker seeds 与 sequelize.js 一起使用:基于其他列值的列值
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()
}
)), {});
我正在尝试的是我需要分配给 department_type
的同一个 deptType 项目,并根据 department_type
给 department_uid
一个值,但我没有得到想要的结果。 .. 请帮忙
在 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()
};
}), {});
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()
}
)), {});
我正在尝试的是我需要分配给 department_type
的同一个 deptType 项目,并根据 department_type
给 department_uid
一个值,但我没有得到想要的结果。 .. 请帮忙
在 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()
};
}), {});