哪个是关联这些数据库表的更好方法?
Which is the better way of relating these database tables?
我有一个 table Users
和 Matters
。通常只有管理员可以创建问题,所以我在用户和问题之间建立了一对多关系(User.hasMany(问题))和(Matter.belongsTo(用户))
现在在前端,Matter
应该有一个名为 assignees
的多 select 字段,用户从 User
table 可以成为 selected.
我目前的方法是在 Matter
上创建一个列 assignees
,这将是一组用户电子邮件 select 在前端编辑,但前端开发人员认为我应该这样做一组用户 ID,但我认为这不会有效,因为在获取所有事项或更新它们时,每次都需要 运行 查询以使用存储在assignees
专栏(我不完全确定该怎么做)。
另一种选择是 UserMatters
加入 table,但我认为在创建更新和获取所有事项将涉及编写大量代码。
我的问题是,有没有更好的方法来解决这个问题,还是我应该坚持用用户电子邮件填充受让人字段,因为据我所知,这看起来是更好的方法?
N.B: 我正在使用 sequelize(postgres)
所以我所做的不是创建 through/join table,而是前端发送了一个包含受让人 ID 的整数数组。由于受让人也是应用程序上的用户,当我得到一个特定的问题时,我只是循环遍历 assignees 列中的 ID,这是一个数组,并从用户 table 获取用户详细信息,然后我将结果重新分配给该列和 return 资源。
try {
// get the resource
const resource = await getResource(id);
/*looping through the column containing an array of IDs and converting it to numbers(if they are strings)**/
let newArr = resource.assignees.map(id => Number(id));
let newAssignees;
/**fetch all users with the corresponding IDs and wait for that process to be complete**/
newAssignees = await Promise.all(newArr.map(id => getUserById(id)));
/**Returns only an array of objects(as per sequelize)**/
newAssignees.map(el => el.get({ raw: true }));
/**reassign the result**/
resource.assignees = newAssignees;
if(resource)
return res.status(200).json({
status: 200,
resource
})
} else {
return res.status(404).json({
status: 404,
message: 'Resource not found'
});
}
} catch(error){
return res.status(500).json({
status: 500,
err: error.message
})
}
我有一个 table Users
和 Matters
。通常只有管理员可以创建问题,所以我在用户和问题之间建立了一对多关系(User.hasMany(问题))和(Matter.belongsTo(用户))
现在在前端,Matter
应该有一个名为 assignees
的多 select 字段,用户从 User
table 可以成为 selected.
我目前的方法是在 Matter
上创建一个列 assignees
,这将是一组用户电子邮件 select 在前端编辑,但前端开发人员认为我应该这样做一组用户 ID,但我认为这不会有效,因为在获取所有事项或更新它们时,每次都需要 运行 查询以使用存储在assignees
专栏(我不完全确定该怎么做)。
另一种选择是 UserMatters
加入 table,但我认为在创建更新和获取所有事项将涉及编写大量代码。
我的问题是,有没有更好的方法来解决这个问题,还是我应该坚持用用户电子邮件填充受让人字段,因为据我所知,这看起来是更好的方法?
N.B: 我正在使用 sequelize(postgres)
所以我所做的不是创建 through/join table,而是前端发送了一个包含受让人 ID 的整数数组。由于受让人也是应用程序上的用户,当我得到一个特定的问题时,我只是循环遍历 assignees 列中的 ID,这是一个数组,并从用户 table 获取用户详细信息,然后我将结果重新分配给该列和 return 资源。
try {
// get the resource
const resource = await getResource(id);
/*looping through the column containing an array of IDs and converting it to numbers(if they are strings)**/
let newArr = resource.assignees.map(id => Number(id));
let newAssignees;
/**fetch all users with the corresponding IDs and wait for that process to be complete**/
newAssignees = await Promise.all(newArr.map(id => getUserById(id)));
/**Returns only an array of objects(as per sequelize)**/
newAssignees.map(el => el.get({ raw: true }));
/**reassign the result**/
resource.assignees = newAssignees;
if(resource)
return res.status(200).json({
status: 200,
resource
})
} else {
return res.status(404).json({
status: 404,
message: 'Resource not found'
});
}
} catch(error){
return res.status(500).json({
status: 500,
err: error.message
})
}