Я создаю веб-приложение, используя Flask с SQLAlchemy.Мое соединение с базой данных успешно и может видеть имена таблиц.Но при сохранении ссылок на таблицы возникает ключевая ошибка.Вот мой код.База данных - fortune500_db и содержит 2 таблицы: fortune500 и sector_industry.Ценю любую помощь.Спасибо
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
Ниже приведен вывод после запуска Flask app.py
(pythonenv37) C:\Users\indum\OneDrive\Documents\group_project2>python fortune500\app.py
['fortune500', 'sector_industry']
Sector TEXT
Industry TEXT
Revenues DOUBLE PRECISION
Profits DOUBLE PRECISION
Profit_Margin DOUBLE PRECISION
Revenue_Percent DOUBLE PRECISION
Profit_Percent DOUBLE PRECISION
Traceback (most recent call last):
File "C:\Users\indum\Anaconda3\envs\pythonenv37\lib\site-packages\sqlalchemy\util\_collections.py", line 210, in __getattr__
return self._data[key]
KeyError: 'fortune500'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "fortune500\app.py", line 37, in <module>
Fortune500 = Base.classes.fortune500
File "C:\Users\indum\Anaconda3\envs\pythonenv37\lib\site-packages\sqlalchemy\util\_collections.py", line 212, in __getattr__
raise AttributeError(key)
AttributeError: fortune500
Вот схема для fortune500.
------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_sql, и в моем файле данных pandas нет столбца для первичного ключа.
# Loading fortune500 data into postgreSQL table
fortune500_data.to_sql(name='fortune500', if_exists='replace', con=engine, index=False)
Может ли это быть проблемой?