SQLAlchemy抛出表名的键错误。为什么不确定?请guid

2024-05-16 10:40:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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

以下是运行烧瓶后的产量应用程序副本在

^{pr2}$

以下是《财富》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)
    );

架构是在PostgreSQL中创建的。 但数据是用pd.to\U sql命令和pandas数据帧没有主键列。在

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

这会是个问题吗?在


Tags: columnsto数据库databasetablenotcolumn