sqlalchemy.exc.ProgrammingError:(psycopg2.errors.UndefinedTable)关系“用户”不存在

时间:2019-11-10 04:54:25

标签: python postgresql flask sqlalchemy

我目前正在参加cs50web课程,我必须将flask应用程序连接到heroku postgres数据库,我已经使用pgadmin 4创建了表,但无法访问数据。

    import os

    from flask import Flask, session
    from flask_session import Session
    from sqlalchemy import create_engine
    from sqlalchemy.orm import scoped_session, sessionmaker
    from dotenv import load_dotenv

    load_dotenv()


    # Check for environment variable
    if not os.getenv("DATABASE_URL"):
        raise RuntimeError("DATABASE_URL is not set")


    # Set up database
    engine = create_engine(os.getenv("DATABASE_URL"))
    db = scoped_session(sessionmaker(bind=engine))

    rows = db.execute("SELECT * FROM users;")
    print(rows)

这是我回来的错误消息

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

Traceback (most recent call last):
  File "/Users/Bryan/Downloads/project1/tester.py", line 21, in <module>
    rows = db.execute("SELECT * FROM users;")
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 162, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1268, in execute
    clause, params or {}
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 988, in execute
    return meth(self, multiparams, params)
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
    distilled_params,
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    e, statement, parameters, cursor, context
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1466, in _handle_dbapi_exception
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 383, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 128, in reraise
    raise value.with_traceback(tb)
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1244, in _execute_context
    cursor, statement, parameters, context
  File "/Users/Bryan/anaconda3/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 552, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "users" does not exist
LINE 1: SELECT * FROM users;
                      ^

[SQL: SELECT * FROM users;]
(Background on this error at: http://sqlalche.me/e/f405)
[Finished in 6.69s]

pgadmin4 screenshot

1 个答案:

答案 0 :(得分:0)

这只是说没有“用户”表之类的东西。