question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Syntax error in built SQL query on Postgres

See original GitHub issue

Describe the bug On Postgres, if I have a model called User, and I do:

User.objects.get(foo=bar)

The generated SQL expression will cause a syntax error from Postgres:

syntax error at or near "."

The reason is, in the generated expression, we are quote escaping the table name everywhere except in the very last ‘Order by’ statement. I.e we get:

SELECT “user”.id AS id FROM “user” WHERE “user”.foo = ‘bar’ ORDER BY user.id

Not having the table name in quotes in the order by statement causes the syntax error in postgres

Is there a workaround available for now @collerek ?

To Reproduce

  • Setup a postgres app
  • Create a simple User table and model
  • Run the query User.objects.get(foo=bar)

Expected behavior Data is returned as expected

Screenshots If applicable, add screenshots to help explain your problem. image

Versions (please complete the following information):

  • Database backend used (mysql/sqlite/postgress) postgres 12.6
  • Python version 3.9.2
  • ormar version 0.10.0
  • pydantic version 1.8
  • if applicable fastapi version 0.63.0

Additional context

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
collerekcommented, Mar 23, 2021

@aliakhtar right now - no.

The easiest way to get this issue away is change the tablename to non reserved sql keyword (as this is the source of the error), i.e. ‘users’ instead of ‘user’.

0reactions
collerekcommented, Mar 23, 2021

Fixed in 0.10.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Syntax error in a simple SQL query [closed]
I am using pgAdmin to write a simple SQL INNER JOIN query. However, when I run the code it only will run the...
Read more >
syntax error at or near ":" when running parametrized query ...
Show activity on this post. command must be either a command string that is completely parsable by the server (i.e., it contains no...
Read more >
Postgre SQL errors – common codes and messages - Paessler
This error indicates either that the requested column does not it exist, or that the query is not correct. There are many possible...
Read more >
15: 38.5. Query Language (SQL) Functions - PostgreSQL
SQL functions execute an arbitrary list of SQL statements, returning the result of the last ... SELECT new_emp().name; ERROR: syntax error at or...
Read more >
Debugging SQL syntax errors - Metabase
Reading an error message shouldn't feel like solving a riddle. This debugging guide explains what you can do about stubborn queries that refuse...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found