Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert with foreign keys with @RelationId() decorator #3867

Open
sykorax opened this issue Mar 21, 2019 · 5 comments
Open

Insert with foreign keys with @RelationId() decorator #3867

sykorax opened this issue Mar 21, 2019 · 5 comments

Comments

@sykorax
Copy link

sykorax commented Mar 21, 2019

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[x] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[x] 0.2.15 (or put your version here)

Steps to reproduce or a small repository showing the problem:

I'm trying to insert an enity with entity manager by setting foreign relation ids instead of specify the related object directly, like:

... manager.insert(UserTenantRole, {tenantEntityId: entity.id, roleId: role.id});

However turning logging on shown that NULL values are being inserted by the manager. It seems manager absolutely ignores the given values in case of setting such foreign keys (decorated with @RelationId).

My entity:

import { Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn, RelationId } from 'typeorm';
import { Role } from './role.entity';
import { TenantUserEntity } from './tenant-user-entity.entity';

@Entity({name: 'users_roles'})
export class UserTenantRole {

  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ManyToOne(type => TenantUserEntity, entity => entity.tenantRoles, {nullable: false})
  @JoinColumn({name: 'entity_id'})
  tenantEntity: Promise<TenantUserEntity> | TenantUserEntity;
  @RelationId((userTenantRole: UserTenantRole) => userTenantRole.tenantEntity)
  tenantEntityId: string;

  @ManyToOne(type => Role, role => role.userTenantRoles, {eager: true, nullable: false})
  @JoinColumn({name: 'role_id'})
  role: Role;
  @RelationId((userTenantRole: UserTenantRole) => userTenantRole.role)
  roleId: string;

}

Using legacy relation passing is fine:

... manager.insert(UserTenantRole, {tenantEntity: entity, role: role});
@sykorax sykorax changed the title Insert with foreign keys with @RoleId() decorator Insert with foreign keys with @RelationId() decorator Mar 21, 2019
@pbirsinger
Copy link

Also would like to know solution for this, as I don't want to have to load whole related object every time I need to insert ..

@lilacdev
Copy link

Need solution too. I don't understand why we would need to do 2 requests as we already know relation.

@lisaah
Copy link

lisaah commented Sep 20, 2019

This sounds like #1795

@udayrajMT
Copy link

For those looking for a solution here's a working example -

let product = new Product();
product.category = { id: 1 }; // or new Category().id = 1 or assigned via constructor
product.storageUrl = req.body.storageUrl;

let result = await this.repository.persist(product);

ref: #447 (comment)

@bombillazo
Copy link

Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants