简体   繁体   中英

"psycopg2.errors.UndefinedTable: relation "flights" does not exist"

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()

After i execute python , he send for me: I do not know the reason Error:

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)

I try change "engine = create_engine("postgres://kali:kali@localhost/mydb")" to "engine = create_engine(os.getenv("postgres://kali:kali@localhost:5432/mydb"))" and:

" AttributeError: 'NoneType' object has no attribute '_instantiate_plugins' "

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "flights" does not exist Means that you don't have a table by the name of(flights) in your database.

Create the table like this: CREATE TABLE flights(id SERIAL PRIMARY KEY NOT NULL, origin VARCHAR NOT NULL, destination VARCHAR NOT NULL, duration INTEGER NOT NULL); Then try running your application, it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM