nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.04k stars 533 forks source link

i need some help on OneToOne #738

Open Lightgh-zhs opened 3 years ago

Lightgh-zhs commented 3 years ago

there are two entities in my project, inventory and product ,like this:

@Entity('inventory') export class Inventory { @PrimaryGeneratedColumn() id: number;

@Column({ name: "product_id" })
productId: number;

@Column({ name: "surplus" })
surplus: string;

@Column({ name: "create_by" })
createBy: Date

@Column({ name: "update_by" })
updateBy: Date

@Column({ name: "create_time" })
createTime: Date

@BeforeInsert()
async newDate() {
    this.createTime = new Date()
}

@Column({ name: "update_time" })
updateTime: Date

@OneToOne((type) => Product, (p) => p.id, { eager: true })
@JoinColumn()
product?: Product;

@BeforeInsert()
updateDateTime() {
    this.updateTime = new Date()
}

@BeforeUpdate()
updateDate() {
    this.updateTime = new Date()
}

}

@Entity('product') export class Product {

@PrimaryGeneratedColumn()
id: number;

@Column({ name: "product_name" })
productName: string;

@Column({ name: "product_no" })
productNo: string;

@Column({ name: "product_model" })
productModel: string;

@Column({ name: "product_specifications" })
productSpecifications: string;

@Column({ name: "product_unit" })
productUnit: string;

@Column({ name: "create_by" })
createBy: Date

@Column({ name: "update_by" })
updateBy: Date

@Column({ name: "create_time" })
createTime: Date

@BeforeInsert()
async newDate() {
    this.createTime = new Date()
}

@Column({ name: "update_time" })
updateTime: Date

@BeforeInsert()
updateDateTime() {
    this.updateTime = new Date()
}

@BeforeUpdate()
updateDate() {
    this.updateTime = new Date()
}

}

the controller like this @Crud({ model: { type: Inventory, }, query: { join: { product: { eager: true, allow: ['id'] }, } }, params: { id: { field: 'id', type: 'number', disabled: true }, } }) @Controller('inventory') export class ProductController implements CrudController { constructor(public service: InventoryService) { } }

when i search the Inventory ,i got an error

Error: ER_BAD_FIELD_ERROR: Unknown column 'Inventory.productId' in 'field list'

anyone can help me