Im having trouble thinking through how to populate an n level table(in my case its 3 levels), Im using python to fetch the data from the query but I'm not sure how to approach populating my new table resources since it references itself. Any feedback on the approach will be much appreciated!
After running the following query in my python file I get the following table
SELECT distinct c.table_catalog AS "Database", c.table_schema AS "Schema", c.table_name AS "Table"
FROM information_schema.columns c
WHERE c.table_schema != 'information_schema' AND c.table_schema != 'pg_catalog' AND c.table_schema != 'pg_internal' AND c.table_schema not like '% %'
ORDER BY c.table_schema, c.table_name;
Database Schema Table
____________________________________
dev BigBangTheory SomeTable1
dev BigBangTheory SomeTable2
dev BigBangTheory SomeTable3
dev Walle AnotherTable100
dev Walle AnotherTable200
dev StarWars SpaceTablexxx
dev StarWars SpaceTableyyy
stage BigBangTheory SomeTable1
stage BigBangTheory SomeTable2
stage BigBangTheory SomeTable3
stage Walle AnotherTable100
stage Walle AnotherTable200
stage StarWars SpaceTablexxx
stage StarWars SpaceTableyyy
And I have another table that I want to populate using the above results. The table I want to populate looks like so:
CREATE TABLE IF NOT EXISTS resources
(
"id" SERIAL NOT NULL PRIMARY KEY,
"type" varchar(100) NOT NULL,
"name" varchar(100) NOT NULL,
"parent" int,
FOREIGN KEY (parent) REFERENCES resources (id)
);
so my goal is for table resources to look like this:
id type name parent
____________________________________________________
1 database dev NULL
2 schema BigBangTheory 1
3 table SomeTable1 2
4 table SomeTable2 2
5 table SomeTable3 2
6 schema Walle 1
7 table AnotherTable100 6
8 table AnotherTable200 6
9 schema StarWars 1
10 table SpaceTablexxx 9
11 table SpaceTableyyy 9
12 database stage NULL
13 schema BigBangTheory 12
14 table SomeTable1 13
15 table SomeTable2 13
16 table SomeTable3 13
17 schema Walle 12
18 table AnotherTable100 17
19 table AnotherTable200 17
20 schema StarWars 12
21 table SpaceTablexxx 20
22 table SpaceTableyyy 20
Thank you in advance! All feedback is appreciated <3