question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@OnyToMany relation find empty array

See original GitHub issue

Issue 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:open
  • Created 3 years ago
  • Reactions:4
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
IliaMazcommented, Nov 17, 2022

Bump, @imnotjames could this be given some attention? As @xtrinch said, that approach doesn’t produce expected results on typeorm@latest.

1reaction
imnotjamescommented, Jun 26, 2021

If #7805 is merged it will… sort of fix this.

With that PR, you’d be able to do:

const result = await this.gDatabaseService.manager.getRepository(Shipment).find({
  where: {
    parcels: {
      id: IsNull(),
    }
  },
   relations: ['parcels'],
});

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 wheres. Given I’m trying to limit the size of that PR I’m going to omit that feature for now…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Symfony 4 API Response OneToMany Relation is Empty Array
Addresses and Companies raletions are empty response when i request from api with postman. (Method GET). Company Entity:
Read more >
ActiveObject OneToMany relationship returning an empty array
I have a series on ActiveObject entities in my JIRA plugin. Template extends Entity. The values I have in ComponentOrder are valid.
Read more >
Best Practices for Many-To-One and One ... - Thorben Janssen
The mapping of associations with JPA and Hibernate seems to be easier than it is. Here are several pitfalls and best practices to...
Read more >
Automatically setting empty arrays instead of undefined on ...
If you have an array on an entity model in typeorm you will have to handle ... for GET requests and I like...
Read more >
JPA Tutorial - JPA Query Is Empty Example - Java2s.com
Queries can use IS EMPTY operator or IS NOT EMPTY to check whether a collection association path resolves to an empty collection or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found