Python 表名的SQLAlchemy抛出键错误。不知道为什么?请引导

Python 表名的SQLAlchemy抛出键错误。不知道为什么?请引导,python,sqlalchemy,Python,Sqlalchemy,我正在使用Flask和SQLAlchemy创建一个web应用程序。我与数据库的连接成功,可以看到表名。但在保存表的引用时,会出现键错误。这是我的密码。该数据库是《财富》500_数据库,它有两个表,即《财富》500和《行业》。 谢谢你的帮助。多谢各位 My code # reflect an existing database into a new engine = create_engine("postgresql://<userid>:<passwd>@localh

我正在使用Flask和SQLAlchemy创建一个web应用程序。我与数据库的连接成功,可以看到表名。但在保存表的引用时,会出现键错误。这是我的密码。该数据库是《财富》500_数据库,它有两个表,即《财富》500和《行业》。 谢谢你的帮助。多谢各位

My code

# reflect an existing database into a new 
engine = create_engine("postgresql://<userid>:<passwd>@localhost:5432/fortune500_db")
print(engine.table_names())
inspector = inspect(engine)
columns = inspector.get_columns('sector_industry')
for column in columns:
    print(column["name"], column["type"])
Base = automap_base()

# reflect the tables
Base.prepare(engine, reflect=True)
Base.prepare(engine, reflect=True)

# Save references to each table
Fortune500 = Base.classes.fortune500
Sector_Industry = Base.classes.sector_industry
架构是在PostgreSQL中创建的。 但数据是使用pd.to_sql命令写入的,我的pandas dataframe没有主键列

# Loading fortune500 data into postgreSQL table
fortune500_data.to_sql(name='fortune500', if_exists='replace', con=engine, index=False)

这可能是个问题吗?

您正在调用
Base.prepare(engine,reflect=True)
两次,这是故意的吗?对不起。我去掉了多余的底线。但这无助于删除错误。我还为两个表配置了主键,现在删除“Id”列“排名”是财富500强表的主键,而“行业”是行业表的主键。但仍然得到相同的“关键”错误。看到这篇文章。看来这是根本原因。谢谢
------Create table fortune500 data------
create table fortune500 (
    "Id" serial primary key,
    "Rank" int not null,
    "Title" varchar not null,
    "Employees" int not null,
    "CEO" varchar not null,
    "CEO Title" varchar not null,
    "Sector" varchar not null,
    "Industry" varchar not null,
    "Years_on_Fortune_500_List" varchar not null,
    "City" varchar not null,
    "State" varchar not null,
    "Latitude" numeric not null,
    "Longitude" numeric not null,
    "Revenues" numeric,
    "Revenue_Change" numeric,
    "Profits" numeric,
    "Profit_Change" numeric,
    "Assets" numeric,
    "Mkt_Value_as_of_3/29/18" numeric,
    "Symbol" char(10)
    );
# Loading fortune500 data into postgreSQL table
fortune500_data.to_sql(name='fortune500', if_exists='replace', con=engine, index=False)