altunyurt
altunyurt

Reputation: 2946

Raw mysql query fails on testing but runs correctly with django

I'm writing unittests for a django application. The code I'm testing works properly when triggered from web, but when testing, code fails with DatabaseError: no such column: TRUE on the very same machine.

Following is the code, that fails

sql = """SELECT `ad`.`question_id`
            FROM `ad`
            INNER JOIN `question` ON (`ad`.`question_id` = `question`.`id`)
            INNER JOIN `answer` ON (`question`.`id` = `answer`.`question_id`)
            WHERE `ad`.`is_active` = TRUE
                AND `answer`.`language_id` = %(language_id)s
                AND `ad`.`order` = %(order)s
            """ % {
                "language_id": user.language.id,
                "order": user.state.total
            }


cursor = connection.cursor()
if cursor.execute(sql):
    ...

The sql generated above is as follows.

SELECT `ad`.`question_id`
            FROM `ad`
            INNER JOIN `question` ON (`ad`.`question_id` = `question`.`id`)
            INNER JOIN `answer` ON (`question`.`id` = `answer`.`question_id`)
            WHERE `ad`.`is_active` = TRUE
                AND `answer`.`language_id` = 1
                AND `ad`.`order` = 672

This works properly either executed in mysql shell or by django itself when a user triggers the relevant code from the web. But it fails when tested using django test framework.

I'm running django 1.5.10 on python 2.7.

What am I doing wrong here?

Edit: If TRUE replaced with 1, it works for testing too.

Upvotes: 0

Views: 179

Answers (1)

altunyurt
altunyurt

Reputation: 2946

Well, I was using sqlite as test database. And it seems that sqlite does not have a boolean literal. That's why tests failed initially, but run properly when TRUE replaced with 1. Leaving this here, in case anyone gets stuck in a similar situation.

Upvotes: 1

Related Questions