Search code examples
sqljsonapitypeorm

TypeORM find method returns non-sense data


So I'm trying to make a simple GET route to get some orders from my DB.

My Order entity looks like that :

@Entity('orders')
export class Order extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @CreateDateColumn()
    createdAt: string;

    @Column()
    price: number;

    @ManyToOne(() => Status, (status) => status.order)
    status: Status;

    @OneToMany(
        () => OrderHaveProducts,
        (OrderHaveProducts) => OrderHaveProducts.product
    )
    products: OrderHaveProducts[];
}

And I'm trying to get with each order, the products ordered, that are stored in another table and I'm using the entity called OrderHaveProducts that looks like that:

@Entity('orders_have_products')
export class OrderHaveProducts extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @ManyToOne(() => Order, (order) => order.products)
    @JoinColumn({ name: 'order_id' })
    order: Order;

    @ManyToOne(() => Product, (product) => product.orders)
    @JoinColumn({ name: 'product_id' })
    product: Product;

    @ManyToMany(() => Ingredient)
    @JoinTable({
        name: 'ordered_products_have_removed_ingredients',
        joinColumn: {
            name: 'ordered_product_id',
            referencedColumnName: 'id',
        },
        inverseJoinColumn: {
            name: 'ingredient_id',
            referencedColumnName: 'id',
        },
    })
    removed_ingredients: Ingredient[];

    @ManyToMany(() => Ingredient)
    @JoinTable({
        name: 'custom_products_have_ingredients',
        joinColumn: {
            name: 'ordered_product_id',
            referencedColumnName: 'id',
        },
        inverseJoinColumn: {
            name: 'ingredient_id',
            referencedColumnName: 'id',
        },
    })
    added_ingredients: Ingredient[];
}

In my DB, in my OrderHaveProducts table I have this data: OrderHaveProducts table in PHPMyAdmin

Where product_id 4 is a pizza called "BBQ" and product_id 2 is a pizza called "Margarita".

The issue I have is that when I execute this code:

const orders: Order[] = await Order.find({
    relations: {
        status: true,
        products: {
            product: true,
            removed_ingredients: true,
            added_ingredients: true,
        },
    },
});

The JSON returned is this:

{
    "status": 200,
    "data": {
        "orders": [
            {
                "id": 1,
                "createdAt": "2023-02-11T22:50:18.721Z",
                "price": 27,
                "status": {
                "id": 4,
                "name": "finished"
                },
                "products": []
            },
            {
                "id": 2,
                "createdAt": "2023-02-11T22:50:18.735Z",
                "price": 15,
                "status": {
                    "id": 4,
                    "name": "finished"
                },
                "products": [
                    {
                        "id": 1,
                        "product": {
                            "id": 2,
                            "name": "Margarita",
                            "price": 12,
                            "picture: "https://medias.delarte.fr/media/sys_master/images/hdb/h49/8875895488542.png"
                        },
                        "removed_ingredients": [
                            {
                                "id": 5,
                                "name": "mozzarella",
                                "stock": 50
                            }
                        ],
                        "added_ingredients": []
                    }
                ]
            }
        ]
    }
}

Where we can clearly see that my first order doesn't have any product (when in DB we see that it has 2) and the second order has 1 product (as expected) but it's not the good product.

I tried to only get the data from the OrderHaveProducts table and I successfuly get the data I see in my DB:

{
    "status": 200,
    "data": {
        "orders": [
            {
                "id": 1,
                "order": {
                    "id": 1,
                    "createdAt": "2023-02-11T22:50:18.721Z",
                    "price": 27
                },
                "product": {
                    "id": 2,
                    "name": "Margarita",
                    "price": 12,
                    "picture": "https://medias.delarte.fr/media/sys_master/images/hdb/h49/8875895488542.png"
                }
            },
            {
                "id": 2,
                "order": {
                    "id": 1,
                    "createdAt": "2023-02-11T22:50:18.721Z",
                    "price": 27
                },
                "product": {
                    "id": 4,
                    "name": "BBQ",
                    "price": 15,
                    "picture": "https://cdn.shopify.com/s/files/1/0508/2179/1903/articles/25-Comment_cuire_pizza_barbecue_1500x.jpg?v=1619600472"
                }
            },
            {
                "id": 3,
                "order": {
                    "id": 2,
                    "createdAt": "2023-02-11T22:50:18.735Z",
                    "price": 15
                },
                "product": {
                    "id": 4,
                    "name": "BBQ",
                    "price": 15,
                    "picture": "https://cdn.shopify.com/s/files/1/0508/2179/1903/articles/25-Comment_cuire_pizza_barbecue_1500x.jpg?v=1619600472"
                }
            }
        ]
    }
}

If anyone can tell me what is going on with all of this it would be incredible, thanks !


Solution

  • I finally found the issue (issues ?). I actually had wrongly set up my relations between my Order entity and OrderHasProducts.

    So in my Order entity my relation was like that:

    @OneToMany(
        () => OrderHaveProducts,
        (OrderHaveProducts) => OrderHaveProducts.product // <-- Here I linked the entity to the product, when I should have linked it to order
    )
    products: OrderHaveProducts[];
    

    So the good relation should have been this:

    @OneToMany(
        () => OrderHaveProducts,
        (OrderHaveProducts) => OrderHaveProducts.order
    )
    products: OrderHaveProducts[];
    

    And the second issue was coming from the OrderHaveProducts entity where I, another time, miss linked the entities, so I had this :

    @ManyToOne(() => Order, (order) => order.products)
    @JoinColumn({ name: 'order_id' })
    order: Order;
    
    @ManyToOne(() => Product, (product) => product.orders)
    @JoinColumn({ name: 'product_id' })
    product: Product;
    

    When the good relation should have been this:

    @Column()
    order_id: number;
    
    @Column()
    product_id: number;
    
    @ManyToOne(() => Order, (order) => order.id)
    @JoinColumn({ name: 'order_id' })
    order: Order;
    
    @ManyToOne(() => Product, (product) => product.id)
    @JoinColumn({ name: 'product_id' })
    product: Product;
    

    So now I successfuly get the good data.

    Hope it can help anyone in the future ;)