英文:
Sequelize include return single record instead all
问题
使用Sequelize和Express.js时,奇怪的是Sequelize返回了单个记录而不是多个。对于单个用户,将许多记录插入了shipping_addresses表中。我不想删除raw:true,因为这个结果属于GraphQL,所以要使用raw:true。
shipping_addresses.belongsTo(users, { as: "userId", foreignKey: "userId" });
users.hasMany(shipping_addresses, { as: "shipping_addresses", foreignKey: "userId" });
sequelize.users.findOne({
    where: {...},
    include: [{ model: shipping_addresses, as: 'shipping_addresses' }],
    raw: true,
    nest: true
}).then(result => {
    // 这里是你期望的结果处理代码
});
这是期望的结果:
{
    id: 1,
    username: "johndeo",
    shipping_addresses: [
        {
            id: 1,
            line: "abc street"
        },
        {
            id: 2,
            line: "another street"
        }
    ]
}
请注意,结果处理部分可能需要根据你的应用程序的需要进行进一步的调整。
英文:
Using sequelize with expressjs, strangely sequelize return single record instead more than one. There are many records are inserted in shipping_addresses table against a single user. I don't want to remove raw:true it's because that result belongs to graphql that's why using raw:true,
shipping_addresses.belongsTo(users, { as: "userId", foreignKey: "userId"});
users.hasMany(shipping_addresses, { as: "shipping_addresses", foreignKey: "userId"});
sequelize.users.findOne({
	where:{...},
	include: [model:shipping_addresses, as:'shipping_addresses'],
	raw:true,
	nest:true
}).
Here is result
{ 
	id:1, 
	username:johndeo
	shipping_addresses: {
		id:1
		line:abc street
	}
}
What I am expecting
{ 
	id:1, 
	username:johndeo
	shipping_addresses: [
		{
			id:1
			line:abc street
		},
		{
			id:2
			line:another street
		},
	]
}
答案1
得分: 0
不使用 raw: true 和 nest: true 来处理这种情况。我猜应该可以工作。
英文:
try not using raw: true and nest: true for this case. I guess it should work
答案2
得分: 0
https://sequelize.org/api/v6/class/src/model.js~Model.html#instance-method-get
您不需要使用 raw: true 选项。相反,调用 get({plain: true})
示例代码
async function test() {
    const row = await Users.findOne({
        attributes: ['id', 'username'],
        where: { id: 1 },
        include: [{ model: ShippingAddresses, attributes: ['id', 'line'], as: 'shipping_addresses' }],
        nest: true
    });
    console.log(row.get({ plain: true }))
}
test()
结果
$ node test
{"pid":25056,"sql":"Executing (default): SELECT \"users\".\"id\", \"users\".\"username\", \"shipping_addresses\".\"id\" AS \"shipping_addresses.id\", \"shipping_addresses\".\"line\" AS \"shipping_addresses.line\" FROM \"users\" AS \"users\" LEFT OUTER JOIN \"shipping_addresses\" AS \"shipping_addresses\" ON \"users\".\"id\" = \"shipping_addresses\".\"user_id\" WHERE \"users\".\"id\" = 1;"}
{
  id: 1,
  username: 'johndeo',
  shipping_addresses: [ { id: 1, line: 'abc street' }, { id: 2, line: 'another street' } ]
}
英文:
https://sequelize.org/api/v6/class/src/model.js~Model.html#instance-method-get
You don't need raw: true option. Instead, call get({plain: true})
Sample Code
async function test() {
    const row = await Users.findOne({
        attributes: ['id', 'username'],
        where: { id: 1 },
        include: [{ model: ShippingAddresses, attributes: ['id', 'line'], as: 'shipping_addresses' }],
        nest: true
    });
    console.log(row.get({ plain: true }))
}
test()
Result
$ node test
{"pid":25056,"sql":"Executing (default): SELECT \"users\".\"id\", \"users\".\"username\", \"shipping_addresses\".\"id\" AS \"shipping_addresses.id\", \"shipping_addresses\".\"line\" AS \"shipping_addresses.line\" FROM \"users\" AS \"users\" LEFT OUTER JOIN \"shipping_addresses\" AS \"shipping_addresses\" ON \"users\".\"id\" = \"shipping_addresses\".\"user_id\" WHERE \"users\".\"id\" = 1;"}
{
  id: 1,
  username: 'johndeo',
  shipping_addresses: [ { id: 1, line: 'abc street' }, { id: 2, line: 'another street' } ]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论