Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:14:28+00:00 2026-05-28T14:14:28+00:00

Django seems to be falsely claiming that I have an error in my SQL

  • 0

Django seems to be falsely claiming that I have an error in my SQL syntax. The query runs fine (returning the intended results) in the django dbshell but spawns an error when the query is run through Django. Here is the code (tracebacks follow):

#this code is inside the models.Customer.display_sharers() function
sharers_by_action_count = Sharer.objects.raw('''
SELECT wordout_sharer.id, COUNT(actions_of_type.id) AS action_count
FROM
wordout_customer
INNER JOIN wordout_sharer
 ON wordout_sharer.customer_id = wordout_customer.id
LEFT JOIN wordout_click
 ON wordout_sharer.id = wordout_click.sharer_id
LEFT JOIN
(SELECT wordout_action.id, wordout_action.click_id
FROM wordout_action
WHERE
wordout_action.action_type_id = %s) as actions_of_type
 ON actions_of_type.click_id = wordout_click.id
WHERE wordout_customer.id = %s
GROUP BY wordout_sharer.id
ORDER BY action_count %s
''', (action_type_id, self.id, direction))

force_execution = list(sharers_by_action_count) #force the query to run by converting it to a list.. this is to trigger the error.

Here is the traceback:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/me/sources/django_wordout/../django_wordout/wordout/models.py", line 106, in display_sharers
    if order_by == 'action_count': #we have to make a special query for when they want to sort by the count of a specific action
  File "/Users/me/sources/django_wordout/../django_wordout/wordout/models.py", line 92, in sharers_by_action_count_with_total_clicks
    force_exec = list(sharers_by_action_count)
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1324, in __iter__
    query = iter(self.query)
  File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 67, in __iter__
    self._execute_query()
  File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 81, in _execute_query
    self.cursor.execute(self.sql, self.params)
  File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/base.py", line 234, in execute
    return Database.Cursor.execute(self, query, params)
DatabaseError: near "?": syntax error

When Database.Cursor.execute(self, query, params) is executed, here are the values of all the parameters:

self:

<django.db.backends.sqlite3.base.SQLiteCursorWrapper object at 0x10c477180> 

query:

        SELECT wordout_sharer.id, COUNT(actions_of_type.id) AS action_count
        FROM
        wordout_customer
        INNER JOIN wordout_sharer
         ON wordout_sharer.customer_id = wordout_customer.id
        LEFT JOIN wordout_click
         ON wordout_sharer.id = wordout_click.sharer_id
        LEFT JOIN
        (SELECT wordout_action.id, wordout_action.click_id
        FROM wordout_action
        WHERE
        wordout_action.action_type_id = ?) as actions_of_type
         ON actions_of_type.click_id = wordout_click.id
        WHERE wordout_customer.id = ?
        GROUP BY wordout_sharer.id
        ORDER BY action_count ?

params:

         (1, 1, 'DESC')

Have I found a bug in Django? Is it not possible for it to deal with certain types of queries correctly?

My configuration: I am running a pretty vanilla development configuration. For the db engine, I’m using sqlite. For migrations (which are up to date), I’m using South.

Update

Just found that replacing the %s‘s with their values makes the query work.. What could be going on to make the %s‘s problematic for Django?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:14:29+00:00Added an answer on May 28, 2026 at 2:14 pm

    According to this page, sqlite treats parameterized values as literals, which means that they insert constants of the type given instead of doing regular “text replacement”.

    Since ASC or DESC in an order by is a keyword, not a constant, it can’t be replaced by a parameter.

    This actually gives some possibly unexpected behavior, if you for example do

    ORDER BY ? DESC
    

    and you give it a column name (say column1), it actually runs but doesn’t sort in your expected order. The reason being that it actually sorts by the string “column1” – which is the same for every row – instead of the actual column content.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I currently have login/ in urls.py redirect to the django.contrib.auth.views.login and that seems
It seems that Django's object model filter method automatically uses the AND SQL keyword.
It seems that Django doesn't have a convention on the placement of template files.
I'm kind of confused by this because it seems that Django templates have optional
That seems simple enough, but all Django Queries seems to be 'SELECT *' How
I'm running fabric (Django deployment to apache) and everything seems to work fine until
I have run into the following error trying to create a user in django:
It appears to me that Django and Pylons have different ideas on how middleware
Django community seems to encourage the use of Postgres, I understand that in a
I have a web report that uses a Django form (new forms) for fields

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.