Within my database, I have a situation where there are two tables sharing relations of the same type.
These tables are named UserCollection
and ImagenProcess
UserCollection has two instances that relate to ImagenProcess. Although the IDs appear unique when viewed individually, incorporating related data leads to duplicate entries.
UserCollection
export class UserCollection extends Model<Partial<UserCollection>> {
// representing user nickname
@Column({
primaryKey: true,
type: DataType.STRING,
allowNull: false,
})
id!: string;
@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
ImagenProcess
export class ImagenProcess extends Model<Partial<ImagenProcess>> {
@Column({
primaryKey: true,
type: DataType.UUID,
allowNull: false,
defaultValue: DataType.UUIDV4
})
id!: string;
...
My approach to fetch, including relevant data is as follows:
const userCollection = await UserCollection.findOne({
where: {
id
},
include: [
'uniqueImage',
'rareImage',
'commonImage',
]
})
However, the retrieved data looks like this (shortened for clarity):
userCollection {
"id": "dc",
"uniqueImageId": "37803940-dad0-45c9-9d74-8cafbe06bc24",
"commonImageId": "e6b5944d-1c24-46fa-8c68-79725ea08514",
"uniqueImage": {
"id": "37803940-dad0-45c9-9d74-8cafbe06bc24",
},
"commonImage": {
"id": "37803940-dad0-45c9-9d74-8cafbe06bc24",
}
}
The values of uniqueImageId
and commonImageId
are distinct from each other:
"uniqueImageId": "37803940-dad0-45c9-9d74-8cafbe06bc24",
"commonImageId": "e6b5944d-1c24-46fa-8c68-79725ea08514",
But the data returned in relation fields seems duplicated...
"uniqueImage": {
"id": "37803940-dad0-45c9-9d74-8cafbe06bc24",
},
"commonImage": {
"id": "37803940-dad0-45c9-9d74-8cafbe06bc24",
}
Could this issue be associated with any known limitations or bugs?
I attempted to simplify the explanation above, but essentially:
One table maintains relationships with named fields pointing towards two instances within another table.
Despite the uniqueness of IDs, duplication arises when utilizing include
.