Skip to content

Typeorm find with where on other side of relation #7251

Closed

Description

@vahidvdn

Documentation Issue

What was unclear or otherwise insufficient?

I have 3 tables as following:

  1. users:

    id

  2. families:

    id

  3. families_users:

    familyId | userId

I have these relations:

// User
@OneToMany(() => FamilyUser, (familyUser) => familyUser.user)
familyUsers: FamilyUser[];

// Family
@OneToMany(() => FamilyUser, (familyUser) => familyUser.family)
familyUsers: FamilyUser[];

// FamilyUser
@ManyToOne(() => User, (user) => user.familyUsers, { nullable: false })
user: User;

@ManyToOne(() => Family, (family) => family.familyUsers, { nullable: false })
family: Family;

I want to get a particular user's family list. The first option is:

await this.familiesUsersRepository.find({
  relations: ['family'],
  where: {
    user: { id: 6 },
  },
});

But in this case, I get the list of FamilyUsers which each one contains a family object. But I want to return the list of families that belongs to the user. I tried this:

return await this.familiesRepository.find({
  relations: ['familyUsers', 'familyUsers.user'],
  where: {
    // ???
  },
});

But have no idea what I should set in the where clause. Any idea?

Recommended Fix

Additional Context

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. changed the title [-]Typeorm find with where on other side of relationship[/-] [+]Typeorm find with where on other side of relation[/+] on Jan 7, 2021
  2. vahidvdn commented on Jul 5, 2021

    @vahidvdn
    Author

    @imnotjames Hi. This pull request refers to "QueryBuilder". Is it available in the repository as well?

  3. imnotjames commented on Jul 5, 2021

    @imnotjames
    Contributor

    Yes, the repository uses a query builder internally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions