Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to find entities with the same many-to-one relationship? #2552

Closed
stanete opened this issue Jul 20, 2018 · 4 comments
Closed

How to find entities with the same many-to-one relationship? #2552

stanete opened this issue Jul 20, 2018 · 4 comments

Comments

@stanete
Copy link

stanete commented Jul 20, 2018

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

Having a many-to-one one-to-many relationship between two entities... Fuel and Station for example:

@Entity()
export class Fuel {
  @PrimaryGeneratedColumn() id: number;

  @Column() price: string;

  @ManyToOne(type => Station, station => station.fuels)
  station: Station;
}
@Entity()
export class Station {
  @PrimaryColumn() id: string;

  @Column() name: string;

  @OneToMany(type => Fuel, fuel => fuel.station, { cascade: true })
  fuels: Array<Fuel>;
}

I want to find all fuels with the same station id.

What I've tried and it doesn't work:

async findAll(stationId: string): Promise<Array<Fuel>> {
        return await this.repository.find({ where: { stationId: stationId } })
}
@stanete stanete changed the title Find entities with the same many-to-one relationship Ho to find entities with the same many-to-one relationship? Jul 20, 2018
@stanete stanete changed the title Ho to find entities with the same many-to-one relationship? How to find entities with the same many-to-one relationship? Jul 20, 2018
@cjackson234
Copy link

cjackson234 commented Jul 20, 2018

You can find nested relationships like so:

getRespository(Fuel).find({ station: { id: stationId } });

@stanete
Copy link
Author

stanete commented Jul 21, 2018

That is correct and works. Thank you.

@stanete stanete closed this as completed Jul 21, 2018
@osnersanchez
Copy link

find nested relationships

I'm trying to do something like that, but I do not get results, I do not know if it has anything to do with the scope of the relationship at the time of the consultation

getRespository(Fuel).find({ station: { city: { id: cityId } } });

@daniel-j-h
Copy link

I had the same issue and added "logging": true" to my config to check out the generated SQL.

Turns out the tables were prefixed with two underscores by typeorm, so I could hack a solution saying

{ __station: { city: { id: cityId } } }

as a where property. Consider this a hack, though, and have a look at the generated SQL first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants