typeorm - Type '{Entity}' is not assignable to type 'QueryDeepPartialEntity<{Entity}>'

Viewed 1853

I am having some difficulties writing typeorm entities for a part of an existing database. This part looks like this:

enter image description here

In short: there's a Brand table, a brand can have more products, the system tries to find pairs of products of different brands. These pairs are stored in the Comparison table that has brand and product ids for each side of the comparison. Plus, it's involved an XTable, a brand can have more Xs and Products and X have a many to many relation.

I am trying to write the entities like this (just the relations):

@Entity('Brands')
class Brand {
    @OneToMany(() => XTable, (x) => x.brand)
    xs: XTable[];

    @OneToMany(() => Product, (product) => product.brand)
    products: Product[];

    @OneToMany(() => Comparisons, (comparison) => comparison.sideABrands)
    sideAComparisons: Comparison[];

    @OneToMany(() => Comparisons, (comparison) => comparison.sideBBrands)
    sideBComparisons: Comparison[];
}

@Entity('Comparisons')
class Comparison {
    @ManyToOne(() => Brand, (brand) => brand.sideAComparisons)
    @JoinColumn({ name: 'SideABrandId', referencedColumnName: 'Id' })
    sideABrand: Brand;

    @ManyToOne(() => Product, (product) => product.sideAComparisons)
    @JoinColumn({ name: 'SideAProductId', referencedColumnName: 'Id' })
    sideAProduct: Product;

    @ManyToOne(() => Brand, (brand) => product.sideBComparisons)
    @JoinColumn({ name: 'SideBBrandId', referencedColumnName: 'Id' })
    sideBBrand: Brand;

    @ManyToOne(() => Product, (product) => product.sideBComparisons)
    @JoinColumn({ name: 'SideBProductId', referencedColumnName: 'Id' })
    sideBProduct: Product;
}

@Entity('Products')
class Product {
    @ManyToMany(() => XTable, (x) => x.products)
    @JoinTable({
        name: 'Products_XTable',
        joinColumn: { name: 'ProductId' },
        inverseJoinColumn: { name: 'XTableId' },
    })
    xs: XTable[];

    @ManyToOne(() => Brand, (brand) => brand.products)
    @JoinColumn({ name: 'BrandId', referencedColumnName: 'Id' })
    brand: Brand;

    @OneToMany(() => Comparison, (comparison) => comparison.sideAProduct)
    sideAComparisons: Comparison[];

    @OneToMany(() => Comparison, (comparison) => comparison.sideBProduct)
    sideBComparisons: Comparison[];
}

@Entity('XTable')
class XTable {
    @ManyToOne(() => Brand, (brand) => brand.xs)
    @JoinColumn({ name: 'BrandId', referencedColumnName: 'Id' })
    brand: Brand;

    @ManyToMany(() => Product, (product) => product.xs)
    @JoinTable({
        name: 'Products_XTable',
        joinColumn: { name: 'XTableId' },
        inverseJoinColumn: { name: 'ProductId' },
    })
    products: Product[];
}

When I try to insert a record in the Comparison table, just doing

getRepository(Comparison).insert(...)

I get an error like:

Argument of type 'Comparison' is not assignable to parameter of type 'QueryDeepPartialEntity<Comparison> | QueryDeepPartialEntity<Comparison>[]'.
  Type 'Comparison' is not assignable to type 'QueryDeepPartialEntity<Comparison>'.
    Types of property 'sideAProduct' are incompatible.
      Type 'Product' is not assignable to type '(() => string) | QueryDeepPartialEntity<Product> | undefined'.
        Type 'Product' is not assignable to type 'QueryDeepPartialEntity<Product>'.
          Types of property 'xs' are incompatible.
            Type 'XTable[]' is not assignable to type '(() => string) | QueryDeepPartialEntity<XTable>[] | undefined'.
              Type 'XTable[]' is not assignable to type 'QueryDeepPartialEntity<XTable>[]'.
                Type 'XTable' is not assignable to type 'QueryDeepPartialEntity<XTable>'.
                  Types of property 'products' are incompatible.
                    Type 'Product[]' is not assignable to type '(() => string) | QueryDeepPartialEntity<Product>[] | undefined'.ts(2345)

I cannot figure out where the error is because the relations look fine to me, if I cast the argument that I pass in the insert function as any it works.

Another question: do these tables look like normalized?

0 Answers
Related