Skip to content

Alias improperly populated in operator conditions #21

@IB21-A

Description

@IB21-A

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @rewiko/crud-typeorm@5.1.12 for the project I'm working on.

Issue:
@rewiko/crud-typeorm generates this query (and other similar ones in different tables) which has operated fine in local and staging environments using SQLite and an older version of MySql, but failed in an environment using MySql 8

SELECT `TargetEvent`.`id` AS `TargetEvent_id`, 
       `TargetEvent`.`slug` AS `TargetEvent_slug`, 
       `TargetEvent`.`displayName` AS `TargetEvent_displayName` 
FROM `target_event` `TargetEvent` 
WHERE (LOWER(targetEvent.slug) LIKE ?)  <---- this is the problem part of the query in production
ORDER BY `TargetEvent_displayName` 
ASC LIMIT 25 -- PARAMETERS: ["%stay%"]

Diagnosis:
in production it seems to require that the WHERE clause be title case (LOWER(TargetEvent.slug) LIKE ?) rather than the generated camel case targetEvent . I have tested and confirmed this directly in the mysql cli interface where the title case version works, and the camelCase version returns an error "Unknown column 'targetEvent.slug' in 'where clause'"

The change in the diff below results in the following query that satisfied my MySql 8 environment:

SELECT `TargetEvent`.`id` AS `TargetEvent_id`, 
       `TargetEvent`.`slug` AS `TargetEvent_slug`, 
       `TargetEvent`.`displayName` AS `TargetEvent_displayName` 
FROM `target_event` `TargetEvent` 
WHERE (LOWER(`TargetEvent`.`slug`) LIKE ?) 
ORDER BY `TargetEvent_displayName` 
ASC LIMIT 25 -- PARAMETERS: ["%stay%"]

Here is the diff that solved my problem:

diff --git a/node_modules/@rewiko/crud-typeorm/lib/typeorm-crud.service.js b/node_modules/@rewiko/crud-typeorm/lib/typeorm-crud.service.js
index 1475728..3e4643d 100644
--- a/node_modules/@rewiko/crud-typeorm/lib/typeorm-crud.service.js
+++ b/node_modules/@rewiko/crud-typeorm/lib/typeorm-crud.service.js
@@ -570,7 +570,10 @@ class TypeOrmCrudService extends crud_1.CrudService {
                 const dbColName = this.entityColumnsHash[field] !== field ? this.entityColumnsHash[field] : field;
                 return `${i}${this.alias}${i}.${i}${dbColName}${i}`;
             case 2:
-                return field;
+                // original code
+                // return field;
+                // Custom code: Ensure alias is included
+                return `${this.alias}.${cols[1]}`;
             default:
                 return cols.slice(cols.length - 2, cols.length).join('.');
         }

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions