Skip to content

missing FROM-clause entry for table "tableName"  #16396

Open

Description

@Falci

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

(Full code)

image
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.findAll works. Post.findOne fails
  • 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.

Activity

  1. added
    pending-approval
    type: bug
    on Aug 18, 2023
  2. TheNoim commented on Sep 4, 2024

    @TheNoim

    findOne under the hood just sets the limit to 1. And I noticed findAll only fails with a limit set.
    I think this is duplicate of #12971

  3. added theissue type on Mar 9, 2025
  4. removed
    type: bug
    on Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approval

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions