First time using TypeORM, so i’m sorry if it’s a very basic issue.
I have two entities: user
and params
:
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true })
email: string;
@Column()
first_name: string;
@Column()
last_name: string;
@Column({ unique: true })
username: string;
@Column()
dob: Date;
@Column()
password: string;
@OneToOne(() => Params, (params) => params.user)
@JoinColumn()
params: Params;
}
import { Entity, PrimaryGeneratedColumn, OneToOne, Column } from 'typeorm';
import { User } from './User';
@Entity()
export class Params {
@PrimaryGeneratedColumn()
id: number;
@Column()
usernameUpdate: Date;
@OneToOne(() => User, (user) => user.params)
user: User;
}
And there is one-to-one relationship between them. But, when i try to fetch params
via user
, it’s always null:
const user = await this.userRepository.findOne({
where: [{ id }],
relations: ['params']
});
User {
id: 2,
...
params: null // here.
}
Since i’m using [email protected], I have data source defined like this:
import 'reflect-metadata';
import { DataSource } from 'typeorm';
import { User } from './entity/User';
import { Params } from './entity/Params';
export const AppDataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'admin',
password: 'admin',
database: 'main-db',
synchronize: true,
logging: false,
entities: [User, Params],
subscribers: []
});
Sync is true and i’ve included params
in entities array, restarted server and everything. Do i have to do manual migrations or what seems to be an issue here?