Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中填充n级PostgreSQL表_Python_Sql_Postgresql_Database Design_Sql Insert - Fatal编程技术网

在python中填充n级PostgreSQL表

在python中填充n级PostgreSQL表,python,sql,postgresql,database-design,sql-insert,Python,Sql,Postgresql,Database Design,Sql Insert,我很难思考如何填充一个n级表(在我的例子中是3级),我使用python从查询中获取数据,但我不确定如何填充我的新表resources,因为它引用自身。如对该方法有任何反馈,将不胜感激 在python文件中运行以下查询后,我得到下表 SELECT distinct c.table_catalog AS "Database", c.table_schema AS "Schema", c.table_name AS "Table" FROM information_schema.columns c W

我很难思考如何填充一个n级表(在我的例子中是3级),我使用python从查询中获取数据,但我不确定如何填充我的新表
resources
,因为它引用自身。如对该方法有任何反馈,将不胜感激

在python文件中运行以下查询后,我得到下表

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
我还有另一个表,我想用上面的结果填充它。 我要填充的表如下所示:

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)
);
因此,我的目标是使表
资源
如下所示:

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

提前谢谢你!所有反馈都被视为一个开端:您可以直接从
信息\u模式表
而不是
信息\u模式列
(每个表只有一行,因此需要
不同的

然后:在Postgres中,您可以在单个查询中执行您想要的操作,使用带有
的级联公共表表达式,将
子句返回到Postgres中的
insert
语句。能

逻辑是首先插入顶级对象(数据库)并返回生成的序列,然后插入模式(使用数据库序列),最后插入表

with 
    info as (
        select c.table_catalog, c.table_schema, c.table_name
        from information_schema.tables
        where 
            c.table_schema not in ('information_schema', 'pg_catalog', 'pg_internal')
            and c.table_schema not like '% %'
    ),
    dbs as (
        insert into resources (type, name)
        select distinct 'database', table_catalog 
        from info
        returning id, name
    ),
    schemas as (
        insert into resources(type, name, parent)
        select distinct 'schema', i.table_schema, d.id
        from info i
        inner join dbs d on d.name = i.table_catalog
        returning id, name, parent
    )
insert into resources(type, name, parent)
select 'table', table_name, s.id
from info i
inner join schemas s on s.name = i.table_schema
inner join dbs d on d.id = s.parent and d.name = i.table_catalog
注意最后一个
insert
schema
dbs
上连接;这是为了正确处理位于不同模式中的“同音异义”表

是一个演示(我使用了一个表来模拟初始查询的结果)