Answer the question
In order to leave comments, you need to log in
How to make in TypeORM, OneToOne relationship without changing the structure of the database?
Good afternoon! There is a DB (the rights only to reading). I describe essences of 2 tables.
1 -User.entity.ts
List of users
import { Entity, PrimaryColumn, Column, Timestamp, OneToOne, JoinColumn } from 'typeorm';
import ModelHasRoles from './modelHasRoles.entity';
@Entity({
name: 'user'
})
export default class User {
@PrimaryColumn({
type: 'integer'
})
id: number;
@OneToOne(() => ModelHasRoles, role => role.modelId)
role: ModelHasRoles
....
}
import { Entity, PrimaryColumn, Column, OneToOne, JoinColumn } from 'typeorm';
import User from './user.entity';
@Entity({
name: 'model_has_roles'
})
export default class ModelHasRoles {
@PrimaryColumn({
name: 'role_id',
type: 'integer'
})
roleId: number;
@OneToOne(() => User, user => user.id)
user: User;
@Column({
name: 'model_type',
type: 'varchar',
length: 255
})
modelType: string;
@Column({
name: 'model_id',
type: 'bigint'
})
modelId: number;
}
User.id == ModelHasRole.modelId
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question