Joining Same Column with more than one table
See original GitHub issueIssue 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:
- Created 4 years ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
I found a workaround. I used leftJoinAndMapOne which seems to work.
Are there any updates when this will be fixed?