Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
PostgreSQL - Syntax error at or near "." #4216
I just transitioned a sequelize project off of sqlite and onto PostgreSql. I'm finding Postgres isn't liking the "SELECT col AS" naming conventions that Sequelize is generating. All of the queries with relationships are being aliased as names with dots in them, which is generating syntax errors. For instance, a basic auth check query for checking whether the active user (account.id = 3) has a task with the code "viewProfile", is generating the following query:
SELECT count(account.id) AS count FROM account AS account INNER JOIN role AS role ON account.roleId = role.id INNER JOIN (roleTasks AS role.tasks.roleTasks INNER JOIN task AS role.tasks ON role.tasks.id = role.tasks.roleTasks.taskId) ON role.id = role.tasks.roleTasks.roleId AND role.tasks.code = 'viewProfile' WHERE account.id = 3;
Which, is generating the following postgres error:
error: [app] SequelizeDatabaseError: syntax error at or near "."
All of this worked fine with the sqlite dialect. Any help would be greatly appreciated.
Here's a better example, I know I haven't included my model definitions - but this is an eager selection of a simple relationship between a "role" table and a "task" table, with a "role_tasks" association table.
Generated PostgreSQL query (missing quotes on inner identifiers):
SELECT role.id, role.code, role.description, role.createdAt, role.updatedAt, tasks.id AS "tasks.id", tasks.code AS "tasks.code", tasks.description AS "tasks.description", tasks.createdAt AS "tasks.createdAt", tasks.updatedAt AS "tasks.updatedAt" FROM role AS role LEFT OUTER JOIN ( roleTasks AS tasks.roleTasks INNER JOIN task AS tasks ON tasks.id = tasks.roleTasks.taskId) ON role.id = tasks.roleTasks.roleId;
Correct PostgreSQL query would be (with quotes around tasks.roleTasks):
SELECT role.id, role.code, role.description, role.createdAt, role.updatedAt, tasks.id AS "tasks.id", tasks.code AS "tasks.code", tasks.description AS "tasks.description", tasks.createdAt AS "tasks.createdAt", tasks.updatedAt AS "tasks.updatedAt" FROM role AS role LEFT OUTER JOIN ( roleTasks AS "tasks.roleTasks" INNER JOIN task AS tasks ON tasks.id = "tasks.roleTasks".taskId) ON role.id = "tasks.roleTasks".roleId;
I cleaned up my models significantly, removing all 'field', 'tableName' and 'as' properties (when defining relationships)... this still didn't fix the issue. I then realized I had quoteIdentifiers set to false, and I changed it to true - and magically it started working!
I'm going to work on reverting my models back to their original definition, to see if the problem was strictly related to quoteIdentifiers, or a combination of issues.
So it kind of feels like quoteIdentifiers is a mandatory option, no? I intentionally set it to false, because I didn't really want to bother quoting all of my mixed-case tables when querying from the command line... but I can obviously live with that.
If you create your table names with lowercase you should still be able to query them fine from the command line (atleast it works for me).