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 backticks #86

Closed
crisward opened this issue Jun 26, 2020 · 2 comments
Closed

MySql backticks #86

crisward opened this issue Jun 26, 2020 · 2 comments
Labels
MySQL wontfix This will not be worked on

Comments

@crisward
Copy link

First of all, thanks for this excellent library. It's a refreshing alternative to ORM's.

There is however one issue which seems to come up very often with mysql, and that's escaping back ticks. As I'm sure you know, Mysql uses them around field names, to avoid clashing with reserved words. However escaping them for use in tagged template literals is just plain ugly.

eg.

 db.query(sql`
    CREATE TABLE \`settings\` (
      \`id\` int(11) unsigned NOT NULL AUTO_INCREMENT,
      \`key\` varchar(64) NOT NULL DEFAULT '',
      \`value\` text,
      PRIMARY KEY (\`id\`),
      UNIQUE KEY \`key\` (\`key\`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  `)

Any ideas on how to improve this?

For my queries I've been aliasing the table to a single letter, then prefixing my fields, if a field name has a dot in it, you don't need back ticks, but that isn't always possible
eg.

 db.query(sql`
    Select * 
    from settings as s
    where
      s.key = "sitename"
  `)
@crisward
Copy link
Author

As an update, it would seem js doesn't have any easy work around for this. But according to the mysql documentation.

If the ANSI_QUOTES SQL mode is enabled, it is also permissible to quote identifiers within double quotation marks:

The ANSI_QUOTES mode causes the server to interpret double-quoted strings as identifiers. Consequently, when this mode is enabled, string literals must be enclosed within single quotation marks. They cannot be enclosed within double quotation marks. The server SQL mode is controlled as described in Section 5.1.11, “Server SQL Modes”.

More info here https://dev.mysql.com/doc/refman/8.0/en/identifiers.html

So there may be a work around by setting a mode.

@ForbesLindesay ForbesLindesay added MySQL wontfix This will not be worked on labels Feb 15, 2022
@ForbesLindesay
Copy link
Owner

I don't think this issue makes sense to address. It's normally not necessary to quote your identifiers, and in the rare cases where you do it is possible to work around the issue either using escaping via \ or using the sql.ident helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MySQL wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants