我正在准备基于UUID与两个表进行联接(我也有id),这些表之间存在一定的困难...
当我运行查询时,我得到了错误。
第一个表称为 users ,他的UUID称为 registry_uuid 第二个表受益人,但是此表有两个UUID, uuid_beneficiary 和 uuid_benefactor 。
为什么?因为第一张表的列为 user_type_id ,因此我们可以知道用户是受益人还是受益人。 第二张表是要知道哪些用户相关。
const User = sequelize.define('users', {
registry_uuid: {
type: Sequelize.UUIDV4,
defaultValue: Sequelize.UUIDV4,
allowNull: false
},
user_type_id: {
type: Sequelize.INTEGER,
defaultValue: 1,
allowNull: false
}
}
const Beneficiary = sequelize.define('beneficiaries', {
uuid_benefactor: {
type: Sequelize.UUIDV4,
defaultValue: Sequelize.UUIDV4,
allowNull: false
},
uuid_beneficiary: {
type: Sequelize.STRING,
defaultValue: Sequelize.UUIDV4,
allowNull: false
},
created_at: {
type: Sequelize.DATE,
defaultValue: Sequelize.NOW
},
disassociation_date: {
type: Sequelize.DATE,
defaultValue: null
}
}
async function getBenefactorOfBeneficiary (benefactorUuid, arrayAttributes) {
arrayAttributes = arrayAttributes || ['registry_uuid', 'name', 'last_name', 'picture']
return UserModel.findOne({
where: {
registry_uuid: {
[Op.eq]: benefactorUuid
}
},
include: [{
model: BeneficiaryModel,
}],
attributes: arrayAttributes,
raw: true
})
}
UserModel.hasMany(beneficiaryModel, {foreignKey: 'uuid_benefactor'})
beneficiaryModel.belongsTo(UserModel,{foreignKey:'registry_uuid'})
我希望输出:
示例:
{
"name": "string",
"lastName": "string",
"picture": "string",
"created_at" "string"
}
很明显,我在控制器中修改了响应