简体   繁体   中英

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "users" does not exist

Im currently taking the cs50web course and i have to connect my flask app to heroku postgres database, i have already created the tables using pgadmin 4 but im not able to access the data.

    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)

This is the error message i got back

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

It is just saying that there is no such thing as a "users" table.

try this:

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

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