Error while performing a COUNT query after filtering on the id of the related model with sqllite3.
See original GitHub issueThe 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:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It seems that everything works fine now. I don’t get any exceptions on the repro code.
I’m travelling this week, I’ll look at it next week. To fix the SQL issue it should be:
I strongly suspect the issue is in PyPika not quoting
*
when there is a table-name involved, or it could be the way we usecount()
in it.