Repository navigation
Open
Labels
pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet
Description
Issue Creation Checklist
- I understand that my issue will be automatically closed if I don't fill in the requested information
- I have read the contribution guidelines
Bug Description
It seems like Sequelize uses some incorrect alias when querying deeply nested tables.
Reproducible Example
Entities definition
@Table
class User extends Model<User> {
@Column
name!: string;
@HasMany(() => Post)
posts!: ReturnType<() => Post[]>;
@HasMany(() => Comment)
comments!: ReturnType<() => Comment[]>;
}
@Table
class Post extends Model<Post> {
@Column
title!: string;
@ForeignKey(() => User)
@Column
userId!: number;
@BelongsTo(() => User)
user!: User;
@HasMany(() => Comment)
comments!: Comment[];
}
@Table
class Comment extends Model<Comment> {
@Column
content!: string;
@ForeignKey(() => Post)
@Column
postId!: number;
@BelongsTo(() => Post)
post!: Post;
@ForeignKey(() => User)
@Column
userId!: number;
@BelongsTo(() => User)
user!: User;
}(async () => {
await sequelize.sync({ force: true });
const { User, Post, Comment } = sequelize.models;
const userWithPosts = {
attributes: ["id"],
include: [
{
model: Post,
attributes: ["id"],
required: true,
},
],
}
// sanity test: find a user with posts works
await User.findOne(userWithPosts);
// Find a post with comments made by authors (user with posts)
await Post.findOne({
attributes: ['id'],
include: [
{
model: Comment,
attributes: ["id"],
required: false,
include: [
{
model: User,
...userWithPosts
},
],
},
],
});
})();What do you expect to happen?
I expect it to select a single post with comments made by an user that also has posts
What is actually happening?
missing FROM-clause entry for table "comments"
Generated SQL
SELECT
"Post".*,
"comments"."id" AS "comments.id",
"comments->user->posts"."id" AS "comments.user.posts.id"
FROM
(
SELECT
"Post"."id",
"comments->user"."id" AS "comments.user.id"
FROM
"Posts" AS "Post"
LEFT OUTER JOIN "Users" AS "comments->user" ON "comments"."userId" = "comments->user"."id"
LIMIT
1
) AS "Post"
LEFT OUTER JOIN "Comments" AS "comments" ON "Post"."id" = "comments"."postId"
INNER JOIN "Posts" AS "comments->user->posts" ON "comments.user.id" = "comments->user->posts"."userId";Notes:
Post.findAllworks.Post.findOnefails- I hope it's ok use to provide a sample using
sequelize-typescript
Environment
- Sequelize version:
yarn list v1.22.19
├─ [email protected]
├─ [email protected]
├─ [email protected]
└─ [email protected]
-
Node.js version: v18.11.0
-
If TypeScript related: TypeScript version:
yarn list v1.22.19
├─ (omitted some irrelevant packages)
├─ [email protected]
└─ [email protected]
- Database & Version: postgres:10.15
- Connector library & Version:
"pg": "^8.10.0"
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I will need guidance.
- No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
- No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.
Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.
Reacted by Kévin Sénéchal and Libright
Metadata
Metadata
Assignees
Labels
pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet
Activity
TheNoim commentedon Sep 4, 2024
findOneunder the hood just sets thelimitto1. And I noticedfindAllonly fails with alimitset.I think this is duplicate of #12971