Crud: Double quoting column name causes error on MySQL

Created on 6 May 2020  路  4Comments  路  Source: nestjsx/crud

Hello,

https://github.com/nestjsx/crud/blob/7ef9744013b4b690fad667ddff089dc9135454d2/packages/crud-typeorm/src/typeorm-crud.service.ts#L791

The referenced code line is causing error when running queries on MySQL. That happens because the column name is enclosed by double quotes, raising MySQL syntax error.

duplicate

Most helpful comment

I have one trick fix it.
Creat curd service custome with code

import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';

export class TypeOrmCustomeCrudService<T> extends TypeOrmCrudService<T> {
  protected getFieldWithAlias(field: string, sort: boolean = false) {
    const cols = field.split('.');
    // relation is alias
    switch (cols.length) {
      case 1:
        if (sort || this.alias[0] === '"') {
          return `${this.alias}.${field}`;
        }
        return `${this.alias}.${field}`;
      case 2:
        return field;
      default:
        return cols.slice(cols.length - 2, cols.length).join('.');
    }
  }
}

Them service exetend it like

@Injectable()
export class UsersService extends TypeOrmCustomeCrudService<UserEntity> {
}

All 4 comments

Pls update. I have like this.

I have one trick fix it.
Creat curd service custome with code

import { TypeOrmCrudService } from '@nestjsx/crud-typeorm';

export class TypeOrmCustomeCrudService<T> extends TypeOrmCrudService<T> {
  protected getFieldWithAlias(field: string, sort: boolean = false) {
    const cols = field.split('.');
    // relation is alias
    switch (cols.length) {
      case 1:
        if (sort || this.alias[0] === '"') {
          return `${this.alias}.${field}`;
        }
        return `${this.alias}.${field}`;
      case 2:
        return field;
      default:
        return cols.slice(cols.length - 2, cols.length).join('.');
    }
  }
}

Them service exetend it like

@Injectable()
export class UsersService extends TypeOrmCustomeCrudService<UserEntity> {
}

@anhduca4 Thank bro. It work to me.

duplicate #401
I'm already on it

Was this page helpful?
0 / 5 - 0 ratings