Developping in nestJS, I am facing an issue using typeORM OneToOne relationship.
The idea:
A user is belonging to a company. It is simple. I set a foreign key in PostgreSQL using PgAdmin.
Here is the first thing that sounds strange to me:
In the following screenshot:
- You can see on the right a previous select * where both the foreign key and column existed.
- You can see on the left that after posting new data, the column used for the foreign key has disappeared. The foreign key constraint disappeared too.
Here is the screenshot:
Here is my code:
app.module.ts:
TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'root',
password: 'root',
database: 'routingfinder',
entities: [Place,User,Company],
synchronize: true,
autoLoadEntities: true,
}), UserModule, AuthModule, PlaceModule, SegmentModule, TeamModule, CompanyModule, RouteModule],
controllers: [AppController],
providers: [AppService, {
provide: APP_INTERCEPTOR,
useClass: CopyrightsInterceptor,
},],
user.entity.ts:
import { Entity, Column, PrimaryGeneratedColumn,OneToOne,JoinColumn} from 'typeorm';
import { Company } from '../../features/company/entities/company.entity';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
username: string;
@Column()
password: String;
@Column({ default: true })
isActive: boolean;
@OneToOne(() => Company)
companyId: number;
}
company.entity.ts:
import { Entity, Column, PrimaryGeneratedColumn} from 'typeorm';
@Entity()
export class Company {
@PrimaryGeneratedColumn()
id: number;
@Column()
companyname: string;
@Column({ default: true })
isActive: boolean;
}
user.service.ts:
...
create(createUsersDto: CreateUserDto): Promise<User> {
const user = new User();
user.username = createUsersDto.username;
user.password= createUsersDto.password;
user.isActive= createUsersDto.isActive;
user.companyId = createUsersDto.companyId;
return this.usersRepository.save(user);
}
...
user.companyId is posted as NULL (whilst on the debugger it is set) and the foreign key constraint disapears in pgAdmin.
Any help/idea would be greatly appreciated.
Thank You
2
Answers
I successfully fixed the problem setting a 'more precise' OneToMany() relationship:
I also modified create-user.dto.ts:
To have CompanyId mapping an Object of Company() type. I did 2 tests at and records are nicely registered with Company Id = 1:
To conclude, I tested with a companyId that does not exist. I am getting an exception.
This is completely correct.
When creating a relation in TypeOrm you want to create a relation between entities, so the type should actually be an Entity of which you want to create a relation. Also you need to specify
@JoinColumn()
on top of the field where you are creating a relationAlso I believe you’ll need to define the relation on your Company entity as well (without the JoinColumn() as the owner of the relation is the
User
)