Opened 8 months ago

Closed 8 months ago

#31609 closed New feature (wontfix)

Add support for Table Function Model.

Reported by: Petr Přikryl Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Petr Přikryl, Ahmad A. Hussein Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Opening based on discussion here https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/django-developers/3HC3twc_pGI/HWzlwAjoAQAJ

Motivation

I have some complex database View with CTEs. The problem here is that I need only few records from that View. But the View is always evaluated completely and limited after that. The solution here is re-writing View as Table Function https://stackoverflow.com/questions/11401749/pass-in-where-parameters-to-postgresql-view.

Proposed usage

class MyTableFunctionModel(Model)
    parent = models.ForeignKey('self', on_delete=models.DO_NOTHING)

    class Meta:
        db_table = 'my_function'
        table_function_args = [
            TableFunctionArg(name='foo', required=False),
            TableFunctionArg(name='bar', required=False),
        ]


MyTableFunctionModel.objects.table_function(foo=1, bar='value', parent__foo=2, parent__bar='cha')

# SELECT ... FROM my_function(1, 'value') T1 JOIN my_function(2, 'cha') T2 ON T1.id = T2.parent_id

Here are some my working experiments https://gist.github.com/petrprikryl/7cd765cd723c7df983de03706bf27d1a

Change History (3)

comment:1 Changed 8 months ago by Petr Přikryl

Cc: Petr Přikryl added

comment:2 Changed 8 months ago by Ahmad A. Hussein

Cc: Ahmad A. Hussein added

comment:3 Changed 8 months ago by Mariusz Felisiak

Resolution: wontfix
Status: newclosed
Summary: Add support for Table Function ModelAdd support for Table Function Model.
Version: master

Thanks for this proposition, however it's quite niche IMO. Moreover if we want to support something in Django we need to support it fully and I can see here many caveats that can cause maintenance burden, e.g. migrations or lack of support on some databases.

It sounds like a third-party database backend is the best way to proceed.

Note: See TracTickets for help on using tickets.
Back to Top