简体   繁体   English

ProgrammingError,带有postgres和sqlalchemy的烧瓶

[英]ProgrammingError, Flask with postgres and sqlalchemy

I am trying to get this setup to work, the database is created correctly, but trying to insert data I get the following error: 我试图使此设置正常工作,数据库创建正确,但是尝试插入数据时出现以下错误:

On sqlite: 在sqlite上:

sqlalchemy.exc.OperationalError
OperationalError: (sqlite3.OperationalError) no such column: Author [SQL: u'SELECT count(*) AS count_1 \nFROM (SELECT Author) AS anon_1']

On postgres: 在postgres上:

sqlalchemy.exc.ProgrammingError
ProgrammingError: (psycopg2.ProgrammingError) column "author" does not exist
LINE 2: FROM (SELECT Author) AS anon_1
                     ^
 [SQL: 'SELECT count(*) AS count_1 \nFROM (SELECT Author) AS anon_1']

edit: Perhaps this has to do with it: I don't understand why it says "anon_1", as I am using credentials clearly? 编辑:也许与此有关:我不明白为什么它说“ anon_1”,因为我清楚地使用了凭据?

I have inspected postgres and sqlite and the tables are created correctly. 我检查了postgres和sqlite,并正确创建了表。 It seems to be an ORM configuration error, as it only seems to happend on inspecting or creating entries, any suggestion would be welcome! 这似乎是ORM配置错误,因为它似乎仅在检查或创建条目时发生,因此欢迎提出任何建议!

class Author(CommonColumns):
    __tablename__ = 'author'
    author = Column(String(200))
    author_url = Column(String(2000))
    author_icon = Column(String(2000))
    comment = Column(String(5000))


registerSchema('author')(Author)


SETTINGS = {
    'SQLALCHEMY_TRACK_MODIFICATIONS': True,
    'SQLALCHEMY_DATABASE_URI': 'sqlite:////tmp/test.db',
    # 'SQLALCHEMY_DATABASE_URI': 'postgresql://xxx:xxx@localhost/test',
}


application = Flask(__name__)

# bind SQLAlchemy
db = application.data.driver
Base.metadata.bind = db.engine
db.Model = Base
db.create_all()

if __name__ == "__main__":
    application.run(debug=True)

What is the query you're using to insert data? 您用来插入数据的查询是什么?

I think the error messages may be a bit more opaque than they need to be because you're using Author/author in three very similar contexts: 我认为错误消息可能比需要的更加不透明,因为您在三种非常相似的情况下使用Author / author:

  • the Class name 类名
  • the table name 表名
  • the column name 列名

For easier debugging, the first thing I'd do is temporarily make each one unique ( AuthorClass , author_table , author_column ) so you can check which 'Author' is actually being referred to by the error message. 为了AuthorClass调试,我要做的第一件事是暂时使每个AuthorClass都唯一( AuthorClassauthor_tableauthor_column ),以便您可以检查错误消息实际上是指哪个“ Author”。

Since you're using the ORM, I suspect the underlying issue is that your insert statement uses Author (the object) when it should actually be using Author.author (the attribute/column name). 由于您使用的是ORM,因此我怀疑潜在的问题是,您的插入语句实际上应使用Author.author (属性/列名称)时使用Author (对象)。 The SELECT statements are complaining that they can't find the column 'author', but because you use author for both the table and column name, it's unclear what's actually being passed into the SQL statement. SELECT语句抱怨它们找不到列“ author”,但是由于您在表名和列名中都使用了author ,因此不清楚在SQL语句中实际传递了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sqlalchemy/Flask/Postgres 编程错误:<unprintable ProgrammingError object> - Sqlalchemy/Flask/Postgres ProgrammingError: <unprintable ProgrammingError object> 烧瓶的sqlalchemy.exc.ProgrammingError - sqlalchemy.exc.ProgrammingError for flask 使用Flask和Postgres的SQLALchemy DB会话 - SQLALchemy DB Session with Flask, Postgres 如何使用Flask-sqlalchemy.exc.ProgrammingError将数据插入Postgresql数据库? - How to insert data to postgresql database with flask - sqlalchemy.exc.ProgrammingError? Flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“users”不存在 - Flask : sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation “users” does not exist sqlalchemy.exc.ProgrammingError Flask SQLAlchemy 无法从我的表中读取数据 - sqlalchemy.exc.ProgrammingError Flask SQLAlchemy cannot read data from my table flask sqlalchemy使Postgres事务保持空闲 - flask sqlalchemy keeps Postgres transaction idle 使用带有postgres的flask sqlalchemy更新状态将不会提交到数据库 - update state with flask sqlalchemy with postgres will not commit to database 如何使用flask-sqlalchemy更新Postgres模式? - How to update a Postgres schema with flask-sqlalchemy? SqlAlchemy(Postgres + Flask):如何求和多列? - SqlAlchemy (Postgres + Flask ) : How to sum multiple columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM