Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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如何根据数据库表的约束关系自动更改数据库表列表的顺序?_Python_Postgresql_List_Networkx - Fatal编程技术网

Python如何根据数据库表的约束关系自动更改数据库表列表的顺序?

Python如何根据数据库表的约束关系自动更改数据库表列表的顺序?,python,postgresql,list,networkx,Python,Postgresql,List,Networkx,要使用Python和PostgreSQL为数据库生成合成数据,我需要确保从给定的表条目列表中,我可以按以下顺序识别表: 优先级->仅包含主键的所有表 优先级->具有主键和外键的所有表 优先级->仅包含一个或多个外键的所有表 对于具有PK和FK(2.优先级)的表,还必须确保其FK已在1中生成的表。优先考虑 e、 g.以下条目列表和约束列表由postgreSQL提供 entry_list = [atp_matches,atp_players,ort,atp_rankings,stadion] co

要使用Python和PostgreSQL为数据库生成合成数据,我需要确保从给定的表条目列表中,我可以按以下顺序识别表:

  • 优先级->仅包含主键的所有表
  • 优先级->具有主键和外键的所有表
  • 优先级->仅包含一个或多个外键的所有表
  • 对于具有PK和FK(2.优先级)的表,还必须确保其FK已在1中生成的表。优先考虑

    e、 g.以下条目列表和约束列表由postgreSQL提供

    entry_list = [atp_matches,atp_players,ort,atp_rankings,stadion]
    
    constraint_list contains: TABLE_NAME, COLUMN_NAME, CONSTRAINT TYPE, REFERENCED TABLE_NAME, REFERENCED COLUMN_NAME
    
    constraint_list = [('atp_players','player_id','primary_key')
                      ,('ort','ort_id','primary_key')                   
                      ,('atp_matches','id','primary_key')
                      ,('atp_matches','stadion_id','foreign_key','stadion','stadion_id')
                      ,('atp_rankings','winner_id','foreign_key','atp_players','player_id')
                      ,('atp_rankings','loser_id','foreign_key','atp_players','player_id')`
                      ,('atp_rankings','id','foreign_key''atp_matches','id')`
                      ,('stadion','stadion_id','primary_key')
                      ,('stadion','ort_id','foreign_key','ort','ort_id')
    
    The correct order would now be:
     1.Priority
        > atp_players (has PK)
        > ort (has PK)
     2.Priority 
        > stadion (has PK&FK) --> FK (ort_id) is already generated
        > atp_matches (PK&FK) --> FK (stadion_id) is already generated
        > atp_rankings (PK&FK) --> FK's (winner_id, loser_id, id are generated) are already generated
     3.Priority
        > not available in that case
    
    result_list = [atp_players,ort,stadion,atp_matches,atp_rankings]
    
    我是Python新手,不知道从给定列表中按正确顺序选择表的合适解决方案是什么。我已经读了一些关于Python模块networkx的文章,但是还没有看到与我的问题类似的地方

    谢谢你的帮助