CSNW / sql-bricks

Transparent, Schemaless SQL Generation
http://csnw.github.io/sql-bricks
MIT License
203 stars 25 forks source link

why can't I use "user" as the name of table #114

Closed BugKun closed 4 years ago

BugKun commented 4 years ago
sqlBricks.select().from('user').where({'id': 1}).toString()
// SELECT * FROM "user" WHERE id = 1
sqlBricks.select().from('users').where({'id': 1}).toString()
// SELECT * FROM users WHERE id = 1

why it has the quotation mark when i use "user" as the table name? It can not run in mysql.

prust commented 4 years ago

@BugKun: Sorry for the delay responding. The sql-bricks library is a core library focused on the ANSI SQL specification. If you're targeting mysql, the MySQL extension will work better for you: https://github.com/tamarzil/mysql-bricks.

Or, you could copy the line from the MySQL extension that overrides the quote character, changing it from the generic " to MySQL's ` on line 9:

sql._autoQuoteChar = '`';

SQLBrick's list of reserved words is a union of the reserved words from sqlite and postgres (including user). Instead, it should default to a list of reserved words from the specification and should relegate the sqlite/postgres/mysql/etc lists to the relevant extensions (https://github.com/CSNW/sql-bricks/issues/116). That said, it looks like "user" is also a reserved word in mysql: https://dev.mysql.com/doc/refman/8.0/en/keywords.html.