felixmosh/knex-mock-client

Identifier wrapping is incorrect for the MySQL dialect

aryehb opened this issue · 1 comments

By default, Knex uses double quotes to wrap identifiers:

import Knex from 'knex'

const knex = Knex({ client: 'pg' });

const { sql } = knex('table_name').toSQL();

// sql === 'select * from "table_name"'

The MySQL dialect uses backticks to wrap identifiers:

import Knex from 'knex'

const knex = Knex({ client: 'mysql' });

const { sql } = knex('table_name').toSQL();

// sql === 'select * from `table_name`'

This is because each dialect subclasses the base Client class, and the MySQL client provides a custom implementation for wrapping identifiers (see here).

MockClient extends the base Knex.Client, regardless of which dialect is specified. As a result, it uses the default wrapping for identifiers, which is double quotes:

import Knex from 'knex'
import { MockClient } from 'knex-mock-client';

const knex = Knex({
  client: MockClient,
  dialect: 'mysql',
});

const { sql } = knex('table_name').toSQL();

// sql === 'select * from "table_name"'

Because of this, for the MySQL dialect, using MockClient does not return the same SQL as un-mocked Knex.

Thank you for reporting this issue, I didn't noticed it 🤷🏽