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.

Error while performing a COUNT query after filtering on the id of the related model with sqllite3.

See original GitHub issue

The subj. I got the following exception: sqlite3.OperationalError: near “*”: syntax error

For repro, please create the following models:

# models.py
from tortoise import Model, fields


class Contact(Model):
    id = fields.IntField(pk=True)
    phone_no = fields.CharField(30, null=False, default='')
    user = fields.ForeignKeyField('models.User', related_name='contacts',
                                  on_delete=fields.CASCADE, null=False)


class User(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(128, null=False, required=True)

Then run the following script:

from tortoise import run_async, Tortoise

from models import Contact


async def run():
    await Tortoise.init(
        db_url='sqlite://db.sqlite3',
        modules={'models': ['models']}
    )
    await Tortoise.generate_schemas()

    await Contact\
        .filter(user__id=1)\
        .count()


if __name__ == '__main__':
    run_async(run())

The raw query seems to be:

SELECT COUNT("contact".*) FROM "contact" LEFT OUTER JOIN "user" ON "user"."id"="contact"."user_id" WHERE "user"."id"=1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
croshchupkincommented, Apr 1, 2019

It seems that everything works fine now. I don’t get any exceptions on the repro code.

1reaction
grigicommented, Mar 28, 2019

I’m travelling this week, I’ll look at it next week. To fix the SQL issue it should be:

SELECT COUNT("contact"."*") FROM "contact" LEFT OUTER JOIN "user" ON "user"."id"="contact"."user_id" WHERE "user"."id"=1

I strongly suspect the issue is in PyPika not quoting * when there is a table-name involved, or it could be the way we use count() in it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLite Query: Select, Where, LIMIT, OFFSET, Count, Group By
To write SQL queries in an SQLite database, you have to know how the SELECT, FROM, WHERE, GROUP BY, ORDER BY, and LIMIT...
Read more >
python - "no such table" exception - Stack Overflow
Another case wihch can generate the no such table error. If your views.py or similar executes code that tries to access the DB...
Read more >
SQLite.swift Documentation - GitHub
swift code (using the expressions and query above) and the corresponding SQL it generates. try db.run(users.create { t in // CREATE TABLE "users ......
Read more >
SQLite COUNT Function: Count Items In A Group
This tutorial shows you how to use SQLite COUNT function to count number of items in a group. You will learn about COUNT(*)...
Read more >
SQLite FTS3 and FTS4 Extensions
Overview. FTS3 and FTS4 are SQLite virtual table modules that allows users to perform full-text searches on a set of documents.
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