【问题标题】:How to insert data with use repository and relation one-to-many?如何使用存储库和一对多关系插入数据?
【发布时间】:2021-05-09 01:07:33
【问题描述】:

我有两个实体产品和类别 我曾尝试将数据添加到数据库,但我没有收到错误。 但结果出乎我的意料。 结果是成功的,但是当我去mysql并查看相关表时,categoryId字段似乎被标记为null

我的产品实体

export class Product {

  @PrimaryGeneratedColumn({ type: 'bigint', unsigned: true })
  id: number;

  @Column({ type: 'varchar', length: 255 })
  name: string;

  @Column({ type: 'int' })
  price: number;

  @Column({ type: 'varchar', length: 255 })
  code: string;

  @Column({ type: 'timestamp', nullable: true })
  created_at: Date;

  @ManyToOne(type => Category, category => category.products,
    { cascade: ['insert', 'update'],eager:true,onDelete:"CASCADE",onUpdate:"CASCADE" })
  category: Category;
}

我的产品服务

@Injectable()
export class ProductService {
  constructor(
    @InjectRepository(Product)
    private productRepository: Repository<Product>,
  ) {
  }




  create(createProductDto: ProductDto): Promise<Product> {
    const product = new ProductDto();
    product.categoryId =createProductDto.categoryId;
    product.code=createProductDto.code;
    product.name = createProductDto.name;
    product.price = createProductDto.price;
    return  this.productRepository.save(product)
  }


}

【问题讨论】:

    标签: node.js nestjs


    【解决方案1】:

    您需要将categoryId 外键添加到您的Product 实体:

    export class Product {
      //...
      @Column()
      categoryId: number;
    }
    

    【讨论】:

    • 我在产品实体中添加了categoryId,但结果在同一个数据库中被标记为null。
    【解决方案2】:

    通过将类别对象分配给product.category,自动更新Product Table中的categoryId外键。

    我只是在我的场景中添加一个示例......

    <!--User Entity with Many to One relation to User Type>
    @Entity()
    export class User extends BaseEntity {
      @PrimaryGeneratedColumn()
      id: number;
    
      @Column({length: 50})
      firstName: string;
    
      @Column({length: 50})
      lastName: string;
    
      @Column({length: 50})
      address: string;
    
      @Column({length: 50})
      street: string;
    
      @Column({length: 10})
      phoneNumber: string;
    
      @Column({length: 50})
      email: string;
    
      @ManyToOne(() => UserType)
      @JoinColumn()
      userType: UserType;
    
      @Column({ default: false })
      isVerified: number;
    }
    
    <!-Uset Type Entity->
    @Entity()
    export class UserType extends BaseEntity {
        @PrimaryGeneratedColumn()
        id: number;
    
        @Column()
        name: string;
    
        @Column()
        description: string;
    }
    
    
    
    <!--My post data to insert-->
    {
        "firstName": "Hashir",
        "lastName": "Muhammed",
        "address1": "Pulasseri House",
        "address2": "",
        "street": "Pazhaya Lakkidi",
        "phoneNumber": "9539191411",
        "email": "hussainmohd.p@gmail.com",
        "userType": {
            "id": 1,
            "name": "Admin",
            "description": "Admin will have all privileges to add and update application data"
        }
    }
    
    

    那么它应该会自动更新数据库中的userTypeId,不需要为此添加单独的字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-18
      • 2012-09-06
      • 2019-04-10
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多