Python如何根据数据库表的约束关系自动更改数据库表列表的顺序?
要使用Python和PostgreSQL为数据库生成合成数据,我需要确保从给定的表条目列表中,我可以按以下顺序识别表: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
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的文章,但是还没有看到与我的问题类似的地方
谢谢你的帮助