skip to Main Content

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:

enter image description here

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


  1. Chosen as BEST ANSWER

    I successfully fixed the problem setting a 'more precise' OneToMany() relationship:

    @ManyToOne(() => Company, (company)=> company )
    @JoinColumn()  
    company: Company;
    

    I also modified create-user.dto.ts:

    import { Company } from "src/features/company/entities/company.entity";
     
    export class CreateUserDto {
    id: number;
    username: string;
    password: string;
    isActive: boolean;
    companyId: Company;
    }
    

    To have CompanyId mapping an Object of Company() type. I did 2 tests at and records are nicely registered with Company Id = 1: Sample records with foreign Key

    To conclude, I tested with a companyId that does not exist. I am getting an exception.

    This is completely correct.


  2. 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 relation

    @Entity()
    export class User {
      @PrimaryGeneratedColumn()
      id: number;
    
      @Column()
      username: string;
    
      @Column()
      password: String;
    
      @Column({ default: true })
      isActive: boolean;
    
      @OneToOne(() => Company)
      @JoinColumn()
      company: Company;
    }
    

    Also 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)

    @Entity()
    export class Company {
        @PrimaryGeneratedColumn()
        id: number;
      
        @Column()
        companyname: string;
      
        @Column({ default: true })
        isActive: boolean;
    
        @Column(() => User)
        user: User;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search