How can we filter find results depending on relations values?

Viewed 20

I am using NestJS with PostgreSQL.

I have a simple OneToOne relation between 2 repositories: Address and Warehouse.

I am trying to fetch all the addresses that do not have a warehouse relation. I have been trying to use Repository manager instead of the QueryBuilder.

Here are my Entities:

@Entity()
export class Address {
    [key: string]: string | string[] | number | Record<string, unknown> | Date;
    @PrimaryGeneratedColumn({
        type: "bigint",
        name: "address_id",
    })
    id: string;

    @Column({
        nullable: true,
    })
    customerId?: string;

    @Field()
    @OneToOne(() => Warehouse, (warehouse: Warehouse) => warehouse.address, { nullable: true })
    warehouse?: string | null;
}

@Entity()
export class Warehouse {
    @PrimaryGeneratedColumn({
        type: "bigint",
    })
    id: string;

    @Field()
    @Column()
    name: string;

    @Field()
    @Column({ nullable: false })
    merchantId: string;

    @OneToOne(() => Address, { eager: true, cascade: true })
    @JoinColumn()
    address: Address;
}

Trying this code:

    findAllAddresses(@Query() query: any) {
        return this.addressesService.find({
            relations: {
                warehouse: true
            },
            where: {
                warehouse: null
            }
        });
    }

Returns all of my results. Even the ones with the warehouse: null field.

I found and tried with

            where: {
                warehouse: IsNull()
            }

But my query actually returns an error (not in TS):

This relation isn't supported by given find operator

How can I filter on this field/relation?

0 Answers
Related