我有四个表-用户,加拿大,法国,德国-完全独立。我(我相信)将它们添加到数据库中。但是,除了“用户”之外,所有数据库的列似乎都无法查询,尽管它们在那里。 (一个人可以查询它们,但不使用fliter()或all()或first()或对查询结果进行迭代。)我想知道问题是否在于我是否需要使用这个declarative_base东西?
DATABASE_URL = os.environ["DATABASE_URL"]
app = Flask(__name__)
app.secret_key = "real key will go here"
app.config.update(mail_settings)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
db = SQLAlchemy(app)
class Users(db.Model):
__tablename__ = "Users"
id_ = db.Column(db.Integer, primary_key=True)
name_ = db.Column(db.String(100))
email_ = db.Column(db.String(100))
date_ = db.Column(db.Date())
count_ = db.Column(db.Integer)
def __init__(self, email, name, date=datetime.datetime.now().date(), count=0):
self.name_ = name
self.email_ = email
self.date_ = date
self.count_ = count
def __repr__(self):
return '<User %r>' % self.email_
class Canada(db.Model):
__tablename__ = 'Canada'
id_ = db.Column(db.Integer, primary_key=True)
address = db.Column(db.String(100))
def __init__(self, address):
self.address = address
def __repr__(self):
return "<Canadian address %r>" % self.address
class France(db.Model):
__tablename__ = "France"
id_ = db.Column(db.Integer, primary_key=True)
address = db.Column(db.String(100))
def __init__(self, address):
self.address = address
def __repr__(self):
return "<French address %r>" % self.address
class Germany(db.Model):
__tablename__ = "Germany"
id_ = db.Column(db.Integer, primary_key=True)
address = db.Column(db.String(100))
def __init__(self, address):
self.address = address
def __repr__(self):
return "<German address %r>" % self.address
db.create_all()
db.session.commit()
我可以添加用户,然后使用网络表单查询他们,而不会出现任何问题。
但是,当我尝试添加加拿大数据库时(在添加之前我先检查它是否已经存在),我的查询失败:
db.session.query(Canada).filter(Canada.address == address).first()
以下是输出:
2018-09-11T13:49:14.998235+00:00 app[web.1]: Traceback (most recent call last):
2018-09-11T13:49:14.998237+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
2018-09-11T13:49:14.998239+00:00 app[web.1]: response = self.full_dispatch_request()
2018-09-11T13:49:14.998241+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-09-11T13:49:14.998242+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-09-11T13:49:14.998244+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-09-11T13:49:14.998246+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-09-11T13:49:14.998247+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-09-11T13:49:14.998249+00:00 app[web.1]: rv = self.dispatch_request()
2018-09-11T13:49:14.998251+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
2018-09-11T13:49:14.998253+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-09-11T13:49:14.998255+00:00 app[web.1]: File "/app/app.py", line 111, in admin
2018-09-11T13:49:14.998256+00:00 app[web.1]: if db.session.query(Canada).filter(Canada.address == address).first() is None:
2018-09-11T13:49:14.998258+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2888, in first
2018-09-11T13:49:14.998260+00:00 app[web.1]: ret = list(self[0:1])
2018-09-11T13:49:14.998262+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2680, in __getitem__
2018-09-11T13:49:14.998263+00:00 app[web.1]: return list(res)
2018-09-11T13:49:14.998265+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2988, in __iter__
2018-09-11T13:49:14.998267+00:00 app[web.1]: return self._execute_and_instances(context)
2018-09-11T13:49:14.998269+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 3011, in _execute_and_instances
2018-09-11T13:49:14.998270+00:00 app[web.1]: result = conn.execute(querycontext.statement, self._params)
2018-09-11T13:49:14.998272+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 948, in execute
2018-09-11T13:49:14.998274+00:00 app[web.1]: return meth(self, multiparams, params)
2018-09-11T13:49:14.998276+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
2018-09-11T13:49:14.998278+00:00 app[web.1]: return connection._execute_clauseelement(self, multiparams, params)
2018-09-11T13:49:14.998279+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
2018-09-11T13:49:14.998281+00:00 app[web.1]: compiled_sql, distilled_params
2018-09-11T13:49:14.998282+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
2018-09-11T13:49:14.998284+00:00 app[web.1]: context)
2018-09-11T13:49:14.998297+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
2018-09-11T13:49:14.998299+00:00 app[web.1]: exc_info
2018-09-11T13:49:14.998300+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
2018-09-11T13:49:14.998302+00:00 app[web.1]: reraise(type(exception), exception, tb=exc_tb, cause=cause)
2018-09-11T13:49:14.998304+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
2018-09-11T13:49:14.998305+00:00 app[web.1]: context)
2018-09-11T13:49:14.998308+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
2018-09-11T13:49:14.998310+00:00 app[web.1]: cursor.execute(statement, parameters)
2018-09-11T13:49:14.998311+00:00 app[web.1]: ProgrammingError: (psycopg2.ProgrammingError) column Canada.address does not exist
2018-09-11T13:49:14.998313+00:00 app[web.1]: LINE 1: SELECT "Canada".id_ AS "Canada_id_", "Canada".address AS "Ca...
2018-09-11T13:49:14.998315+00:00 app[web.1]: ^
2018-09-11T13:49:14.998318+00:00 app[web.1]: [SQL: 'SELECT "Canada".id_ AS "Canada_id_", "Canada".address AS "Canada_address" \nFROM "Canada" \nWHERE "Canada".address = %(address_1)s \n LIMIT %(param_1)s'] [parameters: {'address_1': u'123 fake st. canada\r', 'param_1': 1}] (Background on this error at: http://sqlalche.me/e/f405)
我尝试了很多事情-将代码移到不同的文件中,添加一些涉及“引擎”的代码,更改加拿大列的名称,更改加拿大的名称-但似乎没有任何问题。我注意到的一件事是,我遵循的所有教程在其数据库中仅使用一个表。我想知道这是否是问题的核心。我一直找不到使用多个独立表的指南。
似乎这些列在那里。
>>> for column in Users.__table__.columns:
... print column
...
Users.id_
Users.name_
Users.email_
Users.date_
Users.count_
>>> mapper = inspect(Users)
>>> for column in mapper.attrs:
... print column
...
Users.id_
Users.name_
Users.email_
Users.date_
Users.count_
>>>
>>> from sqlalchemy import inspect
>>> mapper = inspect(Canada)
>>> for column in mapper.attrs:
... print column
...
Canada.id_
Canada.address
>>> for column in Canada.__table__.columns:
... print column
...
Canada.id_
Canada.address
我不知道为什么我的脚本可以查询“用户”列而不是加拿大的列。
那么SQLAlchemy为什么在其中找不到该列的地方生成查询呢?
2018-09-11T13:49:14.998311+00:00 app[web.1]: ProgrammingError: (psycopg2.ProgrammingError) column Canada.address does not exist
2018-09-11T13:49:14.998313+00:00 app[web.1]: LINE 1: SELECT "Canada".id_ AS "Canada_id_", "Canada".address AS "Ca...
2018-09-11T13:49:14.998315+00:00 app[web.1]: ^
2018-09-11T13:49:14.998318+00:00 app[web.1]: [SQL: 'SELECT "Canada".id_ AS "Canada_id_", "Canada".address AS "Canada_address" \nFROM "Canada" \nWHERE "Canada".address = %(address_1)s \n LIMIT %(param_1)s'] [parameters: {'address_1': u'123 fake st. canada\r', 'param_1': 1}] (Background on this error at: http://sqlalche.me/e/f405)
当我做heroku run python
时,会看到以下内容:
`>>> from app import db, Users, Canada
>>> db.session.query(Users).all()
[<User u'mhd@example.com'>, <User u'md@example.edu'>]
>>> db.session.query(Canada).all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2836, in all
return list(self)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2988, in __iter__
return self._execute_and_instances(context)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 3011, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 948, in execute
return meth(self, multiparams, params)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
compiled_sql, distilled_params
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
context)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
exc_info
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
context)
File "/app/.heroku/python/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column Canada.address does not exist
LINE 1: SELECT "Canada".id_ AS "Canada_id_", "Canada".address AS "Ca...
^
[SQL: 'SELECT "Canada".id_ AS "Canada_id_", "Canada".address AS "Canada_address" \nFROM "Canada"'] (Background on this error at: http://sqlalche.me/e/f405)
Users和Canada之间有一些绝对不同。
答案 0 :(得分:0)
也许您需要检查“加拿大”表中是否存在“地址”字段,因为该日志向您显示了postgres数据库错误:
(psycopg2.ProgrammingError) column Canada.address does not exist
如果表中不存在该字段,则需要运行迁移或使用postgres命令“添加列”(alter table documentantion)手动将其添加到数据库中
我希望这能奏效:)