Greetings, I am currently working on a specific relationship:
https://i.sstatic.net/WTKeO.png
When trying to represent the parent's relationship in my entity, this is what I have:
@Entity({ tableName: 'product_instances' })
export class ProductInstance {
@PrimaryKey()
public readonly serial_number: string;
@Property()
public patrimony_code?: string;
@Enum()
public type: ProductTypes;
@ManyToOne(() => Product, { fieldName: 'product_id' })
public product: Product;
@ManyToOne(() => Contract, { fieldName: 'contract_id' })
public contract: Contract;
@ManyToOne(() => Employee, { fieldName: 'employee_id' })
public employee: Employee;
@OneToMany({
entity: () => ProductInstance,
mappedBy: 'parent',
orphanRemoval: true,
})
public parent = new Collection<ProductInstance>(this);
@Property()
public created_at = new Date();
@Property({ onUpdate: () => new Date() })
public updated_at = new Date();
@Property()
public deleted_at?: Date;
constructor(container: instanceContainer) {
this.serial_number = container.serial_number;
this.patrimony_code = container.patrimony_code;
this.type = ProductTypes[container.type];
this.employee = container.employee;
this.contract = container.contract;
this.product = container.product;
}
static build = (container: instanceContainer): ProductInstance => {
return new ProductInstance(container);
};
}
However, I encountered an error related to the one-to-many relationship:
Both ProductInstance.parent and ProductInstance.parent are defined as owning sides, use 'mappedBy' on one of them
Do you think I should establish both a one-to-many and a many-to-one relationship?