findOne for a ManyToOne relationship
See original GitHub issueIssue type:
[x] question [ ] 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)
Steps to reproduce or a small repository showing the problem:
I am trying to findOne with a where in a related entity, but I can’t.
@Entity()
export class UserEntity extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column({ length: 20 })
username: string
@OneToMany(() => GameMemberEntity, gameMember => gameMember.user)
games: GameMemberEntity[]
}
@Entity()
export class GameEntity extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
@Column({ default: false })
published: boolean
@OneToMany(() => GameMemberEntity, gameMember => gameMember.game)
members: GameMemberEntity[]
}
@Entity()
export class GameMemberEntity extends BaseEntity {
@ManyToOne(() => UserEntity, user => user.games, { primary: true })
user: UserEntity
@ManyToOne(() => GameEntity, game => game.members, { primary: true })
game: GameEntity
@Column({ default: false })
edit: boolean
}
I’m trying use findOne, but members no pass to query parameters.
const repository = await getRepository(GameEntity)
const data = await repository.findOne({
where: { id: 2, members: { user: { id: 5 } } },
relations: ['members', 'members.user'],
})
I expected that
...
WHERE "game_entity"."id" = 2 AND "game_member_entity"."userId" = 5;
But is
...
WHERE "game_entity"."id" = 2;
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:11 (2 by maintainers)
Top Results From Across the Web
FindOne in ManyToMany relation with a custom field ...
I found a way to get all data from the relation using the createQueryBuilder method. this.companyRepository .createQueryBuilder('company') .
Read more >Many-to-one / one-to-many relations - typeorm - GitBook
Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take...
Read more >NestJS: blog-api: findOne, findAll, create... & relation ...
NestJS: blog-api: findOne, findAll, create... & relation ManyToOne | Blog Project V-20 ; Link to repository: Github: https://github.com/ ...
Read more >Associations - Sequelize
The A.hasOne(B) association means that a One-To-One relationship exists between A and B , with the foreign key being defined in the target...
Read more >Database | NestJS - A progressive Node.js framework
Relations # ; One-to-many / Many-to-one, Every row in the primary table has one or more related rows in the foreign table. Use...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
if you are looking for a GameEntity with id: 2, you already found the object you are looking for, the additional GameEntityMember: { user: id } requirement is redundant.
will give you the exact same result as below every time
Just a question, are you using
@nestjsx/crud
? Because I think (in my case) it can come from it…Anyway, I fixed this with:
I hope it will help you! 🖖