tortoise / tortoise/tortoise-orm

`sql(params_inline=True)` doesn't include filter values from subqueries

Open
#1,800 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
When using .sql(params_inline=True) on a query that contains filters with subqueries, the filter values from the subqueries are not included in the generated raw SQL.

Current Behavior

When calling .sql(params_inline=True) on a query with subquery filters, the filter values from the subquery are not included in the final SQL string.

Expected Behavior

The filter values from subqueries should be included in the generated SQL when using params_inline=True.

Minimal Reproducible Example
from tortoise import Model, fields, Tortoise, run_async
from tortoise.expressions import Subquery


class OtherModel(Model):
    id = fields.IntField(pk=True)
    field = fields.CharField(max_length=255)

    class Meta:
        table = "other_model"


class MyModel(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255)

    class Meta:
        table = "my_model"


async def main():
    config = {
        "connections": {"default": "sqlite://:memory:"},
        "apps": {
            "models": {
                "models": ["__main__"],
                "default_connection": "default",
            }
        },
    }
    await Tortoise.init(config=config)
    await Tortoise.generate_schemas()

    # The query
    query = MyModel.filter(id__in=Subquery(OtherModel.filter(field="test_value").values_list("id")))

    # Generate SQL with inline params
    sql = query.sql(params_inline=True)
    print(sql)  # The value "test_value" is not included in the output
    await Tortoise.close_connections()


if __name__ == "__main__":
    run_async(main())
Additional Context
  • Tortoise-ORM version: 0.22.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing MyModel.filter(...).sql(params_inline=True) through the Subquery built from OtherModel.filter(field="test_value").values_list("id"). Reproduce the issue with the SQLite in-memory configuration shown, then verify that the generated SQL includes "test_value" for the subquery filter.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlite
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.