Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
TypeORM OneToOne Relation Joined by Primary Key

I have a Database Schema that includes 3 Tables - Users, Clients and Sellers. The users table is a common table for both Clients & Sellers. I Would like to Map a OneToOne Relation on TypeORM where the FK of Clients and Sellers is the own primary key. So their ids should be the same id of the corresponding User id.

Something equivalents to @MapsId on JAVA JPA Hibernate.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

@Edward thanks for your contriubution..

I Resolved that!!

TypeOrm has a property on the OneToOne annotation, called primary, and if we set it to true, it will map the relation throught the primary key, exactly we get on use @MapsId on JPA Hibernate.

Ex (On Seller or Clients class):

 @OneToOne(() => Usuario, { primary: true, cascade: true })
 @JoinColumn({ name: 'OPD_id' })
 usuario: Usuario;

Its enough to create a generalization relation between classes (e.g User/Seller), joined by their id keys.

over 4 years ago · Santiago Trujillo Relatório

0

Something like @MapsId on JPA Hibernate does not exist in TypeOrm. Hibernate is a much more mature ORM.

In TypeOrm you can achieve the same result as follows:

  1. Use @PrimaryColumn instead of @PrimaryGeneratedColumn on the related tables (sellers & clients in your case)

  2. After @PrimaryColumn add @OneToOne relation to the main table (users in your case), and on @JoinColumn make the column name the same as the @PrimaryColumn name. This will give you a single column for the Primary Key and the Foreign Key relation

  3. Add @BeforeInsert in each of the related table to copy the id from the main table (users) to the primary/foreign key of the other table (sellers & clients)

  4. Use { "cascade": true } on the @OneToOne, so you only need to save the related table, the main table will be saved automatically first within the same transaction, then @BeforeInsert will copy the new key to the related table, then the related table will be saved


 import { Entity, PrimaryGeneratedColumn, Column, 
  OneToOne, JoinColumn, PrimaryColumn, BeforeInsert } from "typeorm";
    
  @Entity()
    export class users {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    username: string;
}

 @Entity()
   export class clients {
    @PrimaryColumn()
    clientid: number;
    @OneToOne(() => users, { "cascade": true })
    @JoinColumn({ name: "clientid" } )  // This matches @PrimaryColumn name
    user: users;

    @BeforeInsert()
    newid() { this.clientid = this.user.id; }

    @Column()
    clientname: string;
}

  @Entity()
    export class sellers {
    @PrimaryColumn()
    sellerid: number;
    @OneToOne(() => users, { "cascade": true })
    @JoinColumn({ name: "sellerid" } )  // This matches @PrimaryColumn name
    user: users;

    @BeforeInsert()
    newid() { this.sellerid = this.user.id; }

    @Column()
    sellername: string;
}

Main code, to save User + Seller:

const connection = await createConnection();
const entityManager = connection.createEntityManager();

const user = new users();
const seller = new sellers();
seller.user = user;

user.username = "Giordano User";
seller.sellername = 'Giordano Seller';

await entityManager.save(seller);  // "user" saves automatically because of "cascade"
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda