Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  87] [ 6]  / answers: 1 / hits: 21014  / 4 Years ago, wed, april 22, 2020, 12:00:00

I use TypeORM, and simply I want to insert a row by using relationId. But it's not working as I expected.


Here is my entity:


@Entity()
export default class Address extends BaseEntity {

@Column({
type: 'varchar',
length: 255,
})
public title: number;

@Column({
type: 'varchar',
length: 2000,
})
public value: number;

@ManyToOne(type => User, user => user)
@JoinColumn({ name: 'userId' })
public user: User;

@RelationId((address: Address) => address.user)
public userId: number;

}

When I try to add like the below example, it adds null userId which I do not expect


{
"title": "My home address",
"value": "Lorep Ipsum Sit Amet",
"userId": 4
}


column


When I change the payload, everything works perfectly.


{
"title": "Ev adresim",
"value": "Nova Suites",
"user": 4
}

I do not want to use a payload like the above. I addicted to define a descriptive variable naming. Thanks for all contribution and all answer from now.


More From » orm

 Answers
78

It looks like this is a TypeORM issue with the @RelationId decorator: https://github.com/typeorm/typeorm/issues/3867.



Instead of using @RelationId you can decorate the userId with @Column, when the name of the JoinColumn is equal to the number column name, TypeORM matches both and you can either set the userId or the user and TypeORM will handle it:



@ManyToOne(type => User, user => user)
@JoinColumn({ name: 'userId' })
public user: User;

@Column()
public userId: number;

[#51014] Tuesday, April 14, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tristab

Total Points: 735
Total Questions: 106
Total Answers: 96

Location: Grenada
Member since Sun, Dec 20, 2020
4 Years ago
;