Skip to content

Specifying properties of JSON column in select option should throw an error #11358

@MGB247

Description

@MGB247

Issue description

Find should throw error or select the specific properties of json column when selected

Expected Behavior

When specific properties of a json column are selected, only those properties should be selected. Or the ORM should throw an error that it can not select specific properties for json columns.

Actual Behavior

When specific properties of a json column are selected with find, it selects the entire column and not the specific properties as can be seen in the query generated:

SELECT `Category`.`id` AS `Category_id`, `Category__Category_posts`.`meta` AS `Category__Category_posts_meta`, `Category__Category_posts`.`id` AS `Category__Category_posts_id` FROM `category` `Category` LEFT JOIN `post` `Category__Category_posts` ON `Category__Category_posts`.`categoryId`=`Category`.`id`

Steps to reproduce

@Entity()
export class Category {
    @PrimaryGeneratedColumn()
    id: number;

    @OneToMany(() => Post, (post) => post.category)
    posts: Post[];
}

@Entity()
export class Post {
    @PrimaryColumn()
    id: number;

    @Column("simple-json", { nullable: true })
    meta?: PostMeta;
}

With a json column in your entity, use the find method as follows:

const categories = await dataSource.manager.find(Category, {
    select: {
        id: true,
        posts: {
            meta: {
                // Only selecting likes field
                likes: true,
            },
        },
    },
    relations: ["posts"],
});

Where meta is:

export interface PostMeta {
    likes: number
    dislikes: number
}

Even though dislikes isn't selected, the output will be:

[
    {
        id: 1,
        posts: [
            {
                meta: {
                    likes: 10,
                    dislikes: 1,
                },
            },
        ],
    },
]

My Environment

Dependency Version
Operating System macOS Sonoma 14.3
Node.js version v16
Typescript version 5.8.2
TypeORM version 0.3.21

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • mysql
  • postgres

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions