Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias improperly populated in operator conditions #21

Open
IB21-A opened this issue Aug 8, 2024 · 0 comments
Open

Alias improperly populated in operator conditions #21

IB21-A opened this issue Aug 8, 2024 · 0 comments

Comments

@IB21-A
Copy link

IB21-A commented Aug 8, 2024

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @rewiko/[email protected] 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant