'TypeOrm Left Join with select. Can't seem to select the join table's columns with query builder
I have created a left join between my two tables 'users' and 'quotes. I want to left join them and select which columns from the 'users' table I want to get. (I don't want to get the password, email and likes).
This is the 'users' entity table:
@Entity('users')
export class User {
//uuid
@PrimaryGeneratedColumn({ name: 'user_id' })
userId: number;
@Column({
name: 'email',
type: 'varchar',
length: 255,
nullable: false,
unique: true,
})
email: string;
@Column({
name: 'first_name',
type: 'varchar',
length: 255,
nullable: false
})
firstName: string;
@Column({
name: 'last_name',
type: 'varchar',
length: 255,
nullable: false
})
lastName: string;
@Column({
name: 'password',
type: 'varchar',
length: 255,
nullable: false,
})
password: string;
@Column("int", { array: true, default: {} })
likes: Number[];
}
This is the 'quotes' entity table:
@Entity('quotes')
export class Quote {
//uuid
@PrimaryGeneratedColumn({ name: 'quote_id' })
quoteId: number;
@Column({
name: 'content',
type: "varchar",
length: 300,
nullable: false
})
content: string;
@Column({
name: 'upvotes',
type: 'int',
nullable: false
})
upvotes: number;
@Column({
name: 'posted',
type: 'timestamptz',
}) // Recommended
posted: Date;
//1:1 relation
//user 1--m quote
@OneToOne(type => User, {
nullable: true,
cascade: true
})
@JoinColumn({
name: 'user_tk'
})
userTk: User;
}
As you can see I have a one-to-many relationship. And I'm trying to get this back with a query builder:
[
{
"quoteId": 1,
"content": "asdasdasdasdasd",
"upvotes": 0,
"posted": "2022-04-25T12:59:25.000Z",
"userTk": {
"userId": 1,
"firstName": "string",
"lastName": "string",
}
}
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
