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

MySQL queries using double quotes instead of backtick #67

Closed
hygison opened this issue Nov 10, 2023 · 8 comments · Fixed by #70
Closed

MySQL queries using double quotes instead of backtick #67

hygison opened this issue Nov 10, 2023 · 8 comments · Fixed by #70

Comments

@hygison
Copy link

hygison commented Nov 10, 2023

When I run a normal findAll

@JsonApi(User, { allowMethod: excludeMethod(['deleteRelationship']), requiredSelectField: false })
export class UsersController extends JsonBaseController<User> {
	@InjectService() public jsonApiService: JsonApiService<User>;
	@Inject(HttpService) protected httpService: HttpService;

	@Get()
	async getAll(@Query() query: QueryParams<User>) {
		console.log(this.jsonApiService);
		return this.jsonApiService.getAll({ query: query });
	}
}

It gives me an error:

[Nest] 349  - 11/10/2023, 8:06:06 AM   DEBUG [ErrorInterceptors] HttpException: Http Exception

When I check the ormLogs I get error in the query:

[2023-11-10T08:14:30.784Z][FAILED QUERY]: SELECT `countResult`.`id` AS `countResult_id` FROM `users` `countResult` INNER JOIN (SELECT `User`.`id` AS `subQueryId` FROM `users` `User` WHERE `User`.`deleted_at` IS NULL ORDER BY `User`.`id` ASC LIMIT 20) `subQuery` ON "subQuery"."subQueryId" = `countResult`.`id` WHERE `countResult`.`deleted_at` IS NULL
[2023-11-10T08:14:30.784Z][QUERY ERROR]: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '."subQueryId" = `countResult`.`id` WHERE `countResult`.`deleted_at` IS NULL' at line 1

When I run the query on the sql directly it gives me an error, but If I just replace " with ``` it works fine.

My configs:

const typeOrmConfig: TypeOrmModuleOptions = {
	type: 'mysql',
	host: process.env.DB_HOST,
	port: parseInt(process.env.DB_PORT),
	username: process.env.DB_USER,
	password: process.env.DB_PASSWORD,
	database: process.env.DB_NAME,
	entities: [join(__dirname, '../entities/*.entity{.ts,.js}')],
	migrations: [join(__dirname, '../migrations/*{.ts,.js}')],
	synchronize: true,
	logging: true,
	logger: 'file',
	maxQueryExecutionTime: 1000,
	retryAttempts: 10,
	retryDelay: 3000

I was checking the library and it seems the error to be on : json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.ts
Where we have: "subQuery"."${subQueryIdAlias}" = ${countAlias}.${primaryColumn}

@klerick
Copy link
Owner

klerick commented Nov 14, 2023

sorry, but I didn't test with Mysql

@btibarra
Copy link

@hygison did you find a solution? I can't access the getAll routes either with mysql

@hygison
Copy link
Author

hygison commented Nov 15, 2023

@btibarra I know how to fix the specific error that I have on the nestjs-json-api library.
But can you show me the error you get?

@klerick I am considering 2 points:

  • Change my db to Postgres
  • Keep using MySQL and ask you permission to update your library when I get errors.

@btibarra
Copy link

@hygison I got the exact same error that you got with the " symbol instead of ` when running the mysql query on the get-all.ts file code.
I'm just starting a new project and was learning how to use this library so I just changed to postgres for the moment and it's working fine.

@klerick
Copy link
Owner

klerick commented Nov 15, 2023

@hygison I'm working on new version and will try to check the compatibility with mysql. but full compatibility will be only with postgress. some operators like 'some' will work with postgress.

@klerick
Copy link
Owner

klerick commented Nov 17, 2023

@hygison Could you check fix?
Could you change some code in code from node_modules?
'node_modules/json-api-nestjs/src/lib/mixin/service/typeorm/methods/get-all/get-all.js#113'
need change

.innerJoin(`(${builder.getQuery()})`, 'subQuery', `"subQuery"."${subQueryIdAlias}" = ${countAlias}.${primaryColumn}`)

to

.innerJoin(`(${builder.getQuery()})`, 'subQuery', `${builder.escape('subQuery')}.${builder.escape(subQueryIdAlias)} = ${countAlias}.${primaryColumn}`)

if will be ok, i push new version with fix.
I do not have Mysql server and Can't check myself%)

@hygison
Copy link
Author

hygison commented Nov 19, 2023

@klerick It worked perfectly. Thank you very much.
Screenshot 2023-11-20 at 2 17 14 AM

Should I close this issue or wait for the push?

Copy link

🎉 This issue is included in version [email protected] 🎉

The release is available on GitHub release

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