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.

Many-to-many relations with custom properties select

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

[ ] latest [ ] @next [x] 0.x.x (0.2.18)

Steps to reproduce or a small repository showing the problem:

Hi! I have many-to-many relations with custom properties. For example:

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

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    age: number;

    @OneToMany(() => StudentClassRoom, sc => sc.student)
    studentClassRoom: StudentClassRoom[];
}

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

  @Column()
  name: string;

  @Column()
  hasDesk: boolean;

  @OneToMany(() => StudentClassRoom, sc => sc.classRoom)
  studentClassRoom: StudentClassRoom[];
}

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

  @ManyToOne(() => Student, student => student.studentClassRoom)
  student: Student;

  @ManyToOne(() => ClassRoom, classRoom => classRoom.studentClassRoom)
  classRoom: ClassRoom;
}

How should my query look like to get something like this:

[
  {
     id: 1,
     firstName: 'Jack',
     age: 22,
     classRoom: [
       {
          id:2,
          name: 'History',
          hasDesk: true
       }
     ]
  },
  {
     id: 2,
     firstName: 'Kate',
     age: 21,
     classRoom: [
       {
          id:1,
          name: 'Art',
          hasDesk: false
       },
       {
          id:3,
          name: 'Math',
          hasDesk: true
       }
     ]
  }
]

I have tried with left/inner join, map but to no avail( Thanks!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:26 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
thellimistcommented, Jun 24, 2021

any update on this?

13reactions
frankjrangelcommented, Nov 26, 2019

Yes, I don’t get why the documentation shows you how to do it but doesn’t tell you how to make the query to get that relation when selecting. Would love to know how to do this properly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM: edit a many-to-many relations with custom properties
Im already using active record, and the relationship data is loaded with user entity find. · The query shows query: UPDATE 'user_roles' SET...
Read more >
Many-to-many relations - typeorm - GitBook
Many-to-many is a relation where A contains multiple instances of B, and B contain multiple instances of A. Let's take for example Question...
Read more >
How to create a custom many to many relationship in TypeORM
We will learn how to make this custom relationship using typeORM. Concepts. First of all let's review some concepts: The purpose of typeORM...
Read more >
Many-to-many relations | TypeORM Docs
Many-to-many relations with custom properties. In case you need to have additional properties in your many-to-many relationship, you have to create a new...
Read more >
Configuring how Relationship Joins
The objects will remain present in the collection until the attribute is expired and ... Many to many relationships can be customized by...
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