Skip to content

No empty array on one to many relationship #3145

@atte-backman

Description

@atte-backman

Issue type:

[x ] question
[x ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

With the following setup, relation does not return an empty array, but an object with all properties set to null:

@Entity()
export class MenuItemPrice {
    @Column()
    price: number;

    @ManyToOne(type => Menu, menu => menu.menuItems, { primary: true })
    @JoinColumn({ name: 'menu_id' })
    menu: Menu;

    @ManyToOne(type => MenuItem, menuItem => menuItem.menus, { primary: true })
    @JoinColumn({ name: 'menu_item_id' })
    menuItem: MenuItem;
}
@Entity()
export class Menu {
    @PrimaryColumn()
    id: number;

    @Type(() => MenuItemPrice)
    @OneToMany(type => MenuItemPrice, menuItemPrice => menuItemPrice.menu)
    menuItems: MenuItemPrice[];
}
@Entity()
export class MenuItem {
    @PrimaryColumn()
    id: number;

    @OneToMany(type => MenuItemPrice, menuItemPrice => menuItemPrice.menuItem)
    menus: MenuItemPrice[];
}

Now, when fetching with e.g. findOne(1) and table Menu has 1 record and table MenuItem has no records, I get the following result, when menuItems property should be just an empty array:

{
    "id": 1,
    "menuItems": [
        {
            "price": null,
            "menuItem": null
        }
    ]
}

Am I doing something wrong or is this expected?

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