[英]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:
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
都唯一( AuthorClass
, author_table
, author_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.