Closed Vijay-Karthick closed 5 years ago
@Vijay-Karthick : This worked for me: Add desc to the sqlalchemy import on line 9:
9 from sqlalchemy import func, desc
and then just replace 'total DESC' with desc('total') in main.py (line 96):
92 def sidebar_data():
93 recent = Post.query.order_by(Post.publish_date.desc()).limit(5).all()
94 top_tags = db.session.query(
95 Tag, func.count(tags.c.post_id).label('total')
96 ).join(tags).group_by(Tag).order_by(desc('total')).limit(5).all()
97
98 return recent, top_tags
Hi,
I was able to follow first two chapters of the book. When I ran the code for the third chapter, I got a server error. Please see output below.
(venv)$flask run
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\werkzeug\serving.py", line 302, in run_wsgi execute(self.server.app) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\werkzeug\serving.py", line 290, in execute application_iter = app(environ, start_response) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask\app.py", line 1997, in call return self.wsgi_app(environ, start_response) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask\app.py", line 1985, in wsgi_app response = self.handle_exception(e) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask\app.py", line 1540, in handle_exception reraise(exc_type, exc_value, tb) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask_compat.py", line 33, in reraise raise value File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask\app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask\app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask_compat.py", line 33, in reraise raise value File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request return self.view_functionsrule.endpoint File "C:\Users\Desktop\Daniel Gaspar, Jack Stouffer - Mastering Flask Web Development - Second Edition-Packt (2018)_Code\Chapter03\main.py", line 105, in home recent, top_tags = sidebar_data() File "C:\Users\Desktop\Daniel Gaspar, Jack Stouffer - Mastering Flask Web Development - Second Edition-Packt (2018)_Code\Chapter03\main.py", line 96, in sidebar_data ).join(tags).group_by(Tag).order_by('total DESC').limit(5).all() File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3161, in all return list(self) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3317, in iter return self._execute_and_instances(context) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\orm\query.py", line 3342, in _execute_and_instances result = conn.execute(querycontext.statement, self._params) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\engine\base.py", line 988, in execute return meth(self, multiparams, params) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1098, in _execute_clauseelement else None, File "", line 1, in
File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 462, in compile return self._compiler(dialect, bind=bind, kw) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 468, in _compiler return dialect.statement_compiler(dialect, self, kw) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 562, in init Compiled.init(self, dialect, statement, kwargs) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 319, in init self.string = self.process(self.statement, compile_kwargs) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 350, in process return obj._compiler_dispatch(self, kwargs) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\visitors.py", line 91, in _compiler_dispatch return meth(self, kw) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 2115, in visit_select text, select, inner_columns, froms, byfrom, kwargs File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 2227, in _compose_select_body text += self.order_by_clause(select, kwargs) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 2286, in order_by_clause order_by = select._order_by_clause._compiler_dispatch(self, kw) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\visitors.py", line 91, in _compiler_dispatch return meth(self, kw) File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 970, in visit_clauselist c._compiler_dispatch(self, kw) for c in clauselist.clauses File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 968, in
s
File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 970, in
c._compiler_dispatch(self, kw) for c in clauselist.clauses
File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\visitors.py", line 91, in _compiler_dispatch
return meth(self, kw)
File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\compiler.py", line 772, in visit_textual_label_reference
"Can't resolve label reference for ORDER BY / GROUP BY.",
File "c:\users\desktop\daniel gaspar, jack stouffer - mastering flask web development - second edition-packt (2018)_code\chapter03\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 4501, in _no_text_coercion
"extra": "%s " % extra if extra else "",
sqlalchemy.exc.CompileError: Can't resolve label reference for ORDER BY / GROUP BY. Textual SQL expression 'total DESC' should be explicitly declared as text('total DESC')