sequelize-typescript
sequelize-typescript copied to clipboard
multiple related items of the same type are returning duplicates
Issue
Multiple related items of the same type are returning duplicates
Versions
"sequelize": "^6.17.0",
"sequelize-typescript": "^2.1.3",
"typescript": "^4.6.3"
Issue type
- [X] bug report
- [ ] feature request
Actual behavior
I have a table with some relations that are of the same type, the data looks like below. However when I fetch these as relations, I get identical data - two copies of the first item.
userCollection {
"id": "dc",
"uniqueImageId": "37803940-dad0-45c9-9d74-8cafbe06bc24",
"commonImageId": "e6b5944d-1c24-46fa-8c68-79725ea08514",
}
eg uniqueImage and commonImage are related fields.
notice the IDs are unique
The table definition (sequelize-typescript) has the following
// @IsUUID(4)
@ForeignKey(() => ImagenProcess)
@Column({
type: DataType.UUID,
allowNull: true,
})
uniqueImageId: string;
@BelongsTo(() => ImagenProcess)
uniqueImage: ImagenProcess
@ForeignKey(() => ImagenProcess)
@Column({
type: DataType.UUID,
allowNull: true,
})
commonImageId: string;
@BelongsTo(() => ImagenProcess)
commonImage: ImagenProcess
And i'm fetching with an include like:
const userCollection = await UserCollection.findOne({
where: {
id
},
include: [
'uniqueImage',
'rareImage',
'commonImage',
]
})
however I get this (edited for brevity):
userCollection {
"id": "dc",
"uniqueImageId": "37803940-dad0-45c9-9d74-8cafbe06bc24",
"commonImageId": "e6b5944d-1c24-46fa-8c68-79725ea08514",
"uniqueImage": {
"id": "37803940-dad0-45c9-9d74-8cafbe06bc24",
},
"rareImage": {
"id": "37803940-dad0-45c9-9d74-8cafbe06bc24",
},
"commonImage": {
"id": "37803940-dad0-45c9-9d74-8cafbe06bc24",
}
}
so note that the raw data uniqueImageId and commonImageId are correct (different from each other) but the returned relation blobs are a duplicate...
Is there some limitation or bug known here?
Expected behavior
to be able to get relations
Steps to reproduce
create a table with multiple named fields that relate to the same type.
Related code
- A sample application via GitHub (Best option, since its much easier for us to investigate, so that we can come back to you more recently)
- A code snippet below (Please make sure, that the snippet at least includes tsconfig and the sequelize options)
see above!
also posted to SO here: https://stackoverflow.com/questions/72646281/sequelize-how-to-find-multiple-related-items-of-the-same-type
Had the same issue, I don't think this is a bug. I think this is mentioned somewhere in the README, basically it can't resolve to which ForeignKey the relationship belongs, so you have to tell it.
BelongsTo(() => Model, "<foreignKeyToWhichThisRelationshipBelongs>") solved my issue.
I agree tho, that throwing an error somewhere when someone has the same Model multiple times without specifying the respective foreignkey, would be good.