“psycopg2.errors.UndefinedTable:关系“航班”不存在”

问题描述 投票:0回答:1
import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine("postgres://kali:kali@localhost/mydb")
db = scoped_session(sessionmaker(bind=engine))

def main():
    flights = db.execute ("SELECT origin, destination, duration FROM flights;").fetchall()
    for flight in flights:
        print(f"{flight.origin} to {flight.destination}, {flight.duration} minutes.")

if __name__ == "__main__":
    main()

我执行 python 后,他给我发送了: 我不知道原因 错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    cursor, statement, parameters, context
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/default.py", line 588, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.UndefinedTable: relation "flights" does not exist
LINE 1: SELECT origin, destination, duration FROM flights
                                                  ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "list.py", line 18, in <module>
    main()
  File "list.py", line 13, in main
    flights = db.execute("SELECT origin, destination, duration FROM flights").fetchall()
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/orm/scoping.py", line 162, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/orm/session.py", line 1278, in execute
    clause, params or {}
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 984, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1103, in _execute_clauseelement
    distilled_params,
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1288, in _execute_context
    e, statement, parameters, cursor, context
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1482, in _handle_dbapi_exception
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    cursor, statement, parameters, context
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/default.py", line 588, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "flights" does not exist
LINE 1: SELECT origin, destination, duration FROM flights
                                                  ^

[SQL: SELECT origin, destination, duration FROM flights]
(Background on this error at: http://sqlalche.me/e/f405)
root@ubuntu:~/lecture3# python3 list2.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    cursor, statement, parameters, context
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/default.py", line 588, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.UndefinedTable: relation "flights" does not exist
LINE 1: SELECT origin, destination, duration FROM flights;
                                                  ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "list2.py", line 15, in <module>
    main()
  File "list2.py", line 10, in main
    flights = db.execute ("SELECT origin, destination, duration FROM flights;").fetchall()
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/orm/scoping.py", line 162, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/orm/session.py", line 1278, in execute
    clause, params or {}
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 984, in execute
    return meth(self, multiparams, params)
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/sql/elements.py", line 293, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1103, in _execute_clauseelement
    distilled_params,
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1288, in _execute_context
    e, statement, parameters, cursor, context
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1482, in _handle_dbapi_exception
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    cursor, statement, parameters, context
  File "/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/default.py", line 588, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "flights" does not exist
LINE 1: SELECT origin, destination, duration FROM flights;
                                                  ^

[SQL: SELECT origin, destination, duration FROM flights;]
(Background on this error at: http://sqlalche.me/e/f405)

我尝试将

"engine = create_engine("postgres://kali:kali@localhost/mydb")"
更改为
"engine = create_engine(os.getenv("postgres://kali:kali@localhost:5432/mydb"))"
并且:

" AttributeError: 'NoneType' object has no attribute '_instantiate_plugins' "
python sqlalchemy psycopg2
1个回答
0
投票

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "flights" does not exist
意味着您的数据库中没有名为(航班)的表。

像这样创建表:

CREATE TABLE flights(id SERIAL PRIMARY KEY NOT NULL, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL);
然后尝试运行您的应用程序,它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.