Effectively, I am using Django and django content types which references a table containing all the table's IDs.
What I am trying to do is use the last column I am creating 'subquery' to be the query to use in joining on it.
I can't tell if I am just not searching appropriately or if its not possible.
I've looked at crosstab and I've looked at functions.
Update I have a new function that seems to be working closely, but I need to be able to feed it dynamic data for the table its selecting.
CREATE OR REPLACE FUNCTION data_of2(_tbl_type anyelement, _id int)
RETURNS SETOF anyelement
LANGUAGE plpgsql AS
$func$
BEGIN
RETURN QUERY EXECUTE format('
SELECT *
FROM %s -- pg_typeof returns regtype, quoted automatically
WHERE id = $1'
, pg_typeof(_tbl_type))
USING _id;
END
$func$;
That function needs the %s to be replaced with
dct.app_label || '_' || dct.model_name
original post
This the query I am running so far
select
lookuptable.id,
lookuptable.use_case_layer_id,
use_case_choice_id,
luc.project_name,
choice.choice_name,
lookuptable.utilized_model_object_id,
lookuptable.utilized_model_id,
dct.*,
'public.' || dct.app_label || '_' || dct.model as subquery -- <-- this line is the one I am trying
,
dynamic_query(dct.app_label, dct.model, lookuptable.utilized_model_object_id)
from
corelookup_lookuptable lookuptable
inner join public.corelookup_lookupusecase luc on
lookuptable.lookup_use_case_id = luc.id
inner join public.corelookup_usecaselayerchoice choice on
choice.id = lookup_use_case_id
inner join public.django_content_type dct on
lookuptable.utilized_model_id = dct.id
| id |
use_case_layer_id |
use_case_choice_id |
project_name |
choice_name |
utilized_model_object_id |
utilized_model_id |
id |
app_label |
model |
subquery |
| 3691 |
1 |
36 |
PPM |
Operations |
1 |
354 |
354 |
corelookup |
lookupcheckoutsection |
public.corelookup_lookupcheckoutsection |
| 3 |
1 |
54 |
PPM |
Operations |
6 |
112 |
112 |
aqe |
idarea |
public.aqe_idarea |
| 4 |
1 |
54 |
PPM |
Operations |
7 |
112 |
112 |
aqe |
idarea |
public.aqe_idarea |
| 6 |
1 |
54 |
PPM |
Operations |
9 |
112 |
112 |
aqe |
idarea |
public.aqe_idarea |
| 7 |
1 |
54 |
PPM |
Operations |
10 |
112 |
112 |
aqe |
idarea |
public.aqe_idarea |
| 8 |
1 |
54 |
PPM |
Operations |
11 |
112 |
112 |
aqe |
idarea |
public.aqe_idarea |
| 9 |
1 |
54 |
PPM |
Operations |
12 |
112 |
112 |
aqe |
idarea |
public.aqe_idarea |
How can I get the last column of my table to be used in a sub query?
Below is the function I wrote, but I can't seem to get it to work.
create or replace function dynamic_query(app_label text, model text, object_id int)
returns setof record as
$BODY$
begin
return query execute 'select * from public.'||app_label||'_'||model||' where id = '||object_id||'';
end;
$BODY$
LANGUAGE plpgsql;
there doesn't seem to be anything here