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.

Joining Same Column with more than one table

See original GitHub issue

Issue type:

[ ] question [x] 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 have got three entities, Contact, EnhancedContact and MappedContact, with a one to one relation between them. The entities are shown below:

@Entity()
export class Contact {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({ name: 'first_name', type: 'text' })
    firstName: string;

    @Column({ name: 'last_name', type: 'text' })
    lastName: string;

    @Column({ name: 'email', type: 'text' })
    email: string;

}

@Entity()
export class MappedContact{
    @PrimaryColumn({type: 'int', name: 'id_contact'})
    contactId: number;

    @OneToOne(type => Contact)
    @JoinColumn({name: 'id_contact'})
    contact: Contact;

    @OneToOne(type => EnhancedContact)
    @JoinColumn({name: 'id_contact'})
    enhancedContact: EnhancedContact;
}


@Entity()
export class EnhancedContact{
    @PrimaryColumn({type: 'int', name: 'id_contact'})
    contactId: number;

    @OneToOne(() => Contact)
    @JoinColumn({name: 'id_contact'})
    contact: Contact;

    @Column({name: 'power', type: 'int'})
    power: number;
}

I want to left join and select contact from mapped contact. For this, I employ the following command:

const mcs = await connection.manager.createQueryBuilder(MappedContact, "entity")
    .leftJoinAndSelect("entity.contact", "contact")
    .getMany();
console.log(mcs);

I get a failed Query error:

SELECT "entity"."id_contact" AS "entity_id_contact", "contact"."id" AS "contact_id", "contact"."first_name" AS "contact_first_name", "contact"."last_name" AS "contact_last_name", "contact"."email" AS "contact_email" FROM "mapped_contact" "entit
y" LEFT JOIN "contact" "contact" ON contact.contactId="entity"."id_contact"

I don’t know why contact.id became contact.contactId.

I also tried referencedColumnName, It still gave me the same error.

@Entity()
export class Contact {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({ name: 'first_name', type: 'text' })
    firstName: string;

    @Column({ name: 'last_name', type: 'text' })
    lastName: string;

    @Column({ name: 'email', type: 'text' })
    email: string;

}

@Entity()
export class MappedContact{
    @PrimaryColumn({type: 'int', name: 'id_contact'})
    contactId: number;

    @OneToOne(type => Contact)
    @JoinColumn({name: 'id_contact', referencedColumnName: 'id'})
    contact: Contact;

    @OneToOne(type => EnhancedContact)
    @JoinColumn({name: 'id_contact', referencedColumnName: 'contactId'})
    enhancedContact: EnhancedContact;
}


@Entity()
export class EnhancedContact{
    @PrimaryColumn({type: 'int', name: 'id_contact'})
    contactId: number;

    @OneToOne(() => Contact)
    @JoinColumn({name: 'id_contact', referencedColumnName: 'id'})
    contact: Contact;

    @Column({name: 'power', type: 'int'})
    power: number;
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
ramk18commented, Mar 28, 2019

I found a workaround. I used leftJoinAndMapOne which seems to work.

0reactions
Kvikikpcommented, Aug 24, 2022

Are there any updates when this will be fixed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL: How to make multiple joins to the same column of a table ...
How do I make a JOIN that retrieves both team names from the teams table? I have tried: SELECT * FROM matches AS...
Read more >
How to Join the Same Table Twice | LearnSQL.com
Multiple Relationships Between two Tables. There are situations beside the self join in which you need to join the same table more than...
Read more >
A SQL join on multiple tables: overview and implementation
To apply join between two tables, one table must contain a column that is a reference for the other table. In the example...
Read more >
Specifying a Join in the WHERE Clause - IBM
You join two tables by creating a relationship in the WHERE clause between at least one column from one table and at least...
Read more >
SQL joins and how to use them - Launch School
A RIGHT JOIN is similar to a LEFT JOIN except that the roles between the two tables are reversed, and all the rows...
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