@OnyToMany relation find empty array
See original GitHub issueIssue type: QUESTION
Hi, can anyone tell me how should I query for empty @OneToMany
relation using find
? The main issue is that I cannot use QueryBuilder because I created a Pagination System for all of those elements. I have 2 entities Shipment
and Parcel
. The shipment has @OneToMany
relation to Parcel
and Parcel
has @ManyToOne
relation to Shipment
. When I run the code:
const result = await this.gDatabaseService.manager.getRepository(Shipment).find({
where: {
parcels: IsNull(),
},
relations: ['parcels'],
});
I always get the same results - in SQL theres no information about the re
[ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
ShipmentEntity
@Entity('shipments')
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
/**
* Parcel entity
*/
export default class ShipmentEntity {
/**
* ID primary column
*/
@PrimaryGeneratedColumn('uuid', { name: 'id' })
public id: string;
@Column({ type: 'enum', enum: EShipmentType })
public type: EShipmentType;
@ManyToOne(() => AddressEntity)
public sender: AddressEntity;
@ManyToOne(() => AddressEntity)
public receiver: AddressEntity;
@OneToMany(() => ParcelEntity, (parcel) => parcel.shipment)
public parcels?: ParcelEntity[];
@Column({ nullable: true })
public comment?: string;
@CreateDateColumn({ type: 'timestamp' })
public createdAt: Date;
@ManyToOne(() => TenantEntity, (tenant) => tenant.id)
public tenant: TenantEntity;
@ManyToOne(() => WarehouseEntity, (warehouse) => warehouse.id)
public warehouse: WarehouseEntity;
}
ParcelEntity
@Entity('parcels')
@TableInheritance({ column: { type: 'varchar', name: 'type' } })
/**
* Parcel entity
*/
export default class ParcelEntity {
/**
* ID primary column
*/
@PrimaryGeneratedColumn('uuid', { name: 'id' })
public id: string;
@ManyToOne(() => ShipmentEntity, (shipment) => shipment.parcels)
public shipment: ShipmentEntity;
/**
* Parcel quantity
*/
@Column({ nullable: false })
public quantity: number;
/**
* Parcel width
*/
@Column({ nullable: false })
public width: number;
/**
* Parcel height
*/
@Column({ nullable: false })
public height: number;
/**
* Parcel length
*/
@Column({ nullable: false })
public length: number;
/**
* Parcel weight
*/
@Column({ type: 'float', nullable: false })
public weight: number;
@CreateDateColumn({ type: 'timestamp' })
public createdAt: Date;
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:10 (3 by maintainers)
Top GitHub Comments
Bump, @imnotjames could this be given some attention? As @xtrinch said, that approach doesn’t produce expected results on typeorm@latest.
If #7805 is merged it will… sort of fix this.
With that PR, you’d be able to do:
To allow for the notation you’re suggesting (which can be used for one-to-one but not one-to-many) it’d require pre-processing when we handle our
where
s. Given I’m trying to limit the size of that PR I’m going to omit that feature for now…