nestjsx / crud

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

QueryFailedError: Error: Invalid column name 'undefined'. #607

Closed kevinsproles closed 4 years ago

kevinsproles commented 4 years ago

using typeorm and mssql

This happens on POST or PUT. It inserts / updates successfully, but the json response is:

{
    "statusCode": 500,
    "message": "Internal server error"
}

And the console log gets:

QueryFailedError: Error: Invalid column name 'undefined'.

have a very simple controller:

@Crud({
  model: {
    type: Categories,
  },
})
@Controller('categories')
export class CategoriesController {
  constructor(public service: CategoriesService) {}
}

I've narrowed down this issue to only happen if the primary key column of the database table is not simply "id" but has some other name. In this case categoryId.

@Entity({ name: 'categories' })
export class Categories {
  @PrimaryGeneratedColumn()
  categoryId: number;
...
kevinsproles commented 4 years ago

After a few hours, found the solution. You need to specify the id field in the @Crud params.id.field like this, and btw it must match exactly case-sensitive to your entity id, in your entity file right below @PrimaryGeneratedColumn():

@Crud({
  model: {
    type: Categories,
  },
  params: {
    id: {
      field: 'categoryId',
      type: 'number',
      primary: true,
    },
  },
})
@Controller('categories')
export class CategoriesController {
  constructor(public service: CategoriesService) {}
}

matching categories.entity.ts:

@Entity({ name: 'categories' })
export class Categories {
  @PrimaryGeneratedColumn()
  categoryId: number;
...
antoooooooooooonie commented 3 years ago

Thank you!