I'm Tim! I like building things 😃
- My Blog https://medium.com/@timmykko
- Personal Website https://timothyko.org
- Linkedin https://www.linkedin.com/in/timothy-ko/
Simple flask boilerplate with Postgres, Docker, and Heroku/Zeit now
Home Page: https://github.com/tko22/flask-boilerplate/wiki
License: MIT License
I'm Tim! I like building things 😃
When running docker-compose build
& docker-compose up
from the zipfile on a mac, I run into the following issue:
app | Traceback (most recent call last):
app | File "manage.py", line 3, in <module>
app | from api import create_app
app | File "/app/api/__init__.py", line 7, in <module>
app | from sqlalchemy_utils import create_database, database_exists
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/__init__.py", line 1, in <module>
app | from .aggregates import aggregated # noqa
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/aggregates.py", line 372, in <module>
app | from .functions.orm import get_column_key
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/__init__.py", line 1, in <module>
app | from .database import ( # noqa
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 10, in <module>
app | from ..expressions import explain_analyze
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/expressions.py", line 4, in <module>
app | from sqlalchemy.sql.expression import (
app | ImportError: cannot import name '_literal_as_text' from 'sqlalchemy.sql.expression' (/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/expression.py)
app | Traceback (most recent call last):
app | File "manage.py", line 3, in <module>
app | from api import create_app
app | File "/app/api/__init__.py", line 7, in <module>
app | from sqlalchemy_utils import create_database, database_exists
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/__init__.py", line 1, in <module>
app | from .aggregates import aggregated # noqa
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/aggregates.py", line 372, in <module>
app | from .functions.orm import get_column_key
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/__init__.py", line 1, in <module>
app | from .database import ( # noqa
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 10, in <module>
app | from ..expressions import explain_analyze
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/expressions.py", line 4, in <module>
app | from sqlalchemy.sql.expression import (
app | ImportError: cannot import name '_literal_as_text' from 'sqlalchemy.sql.expression' (/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/expression.py)
Looks like this happens on the line:
from sqlalchemy_utils import create_database, database_exists
In api/__init__.py
. The solution appears to be to update requirements.txt
with the following change:
sqlalchemy<1.4.0
According to this StackOverflow discussion: https://stackoverflow.com/questions/66644975/importerror-cannot-import-name-columnentity-from-sqlalchemy-orm-query
use app.logger()
First off, I'm a programmer, but have beginner level experience with Python and Docker ;-)
I've got the app running fine in Docker, but am trying to figure out the development process.
I've reviewed the docs, and blog post, but am not quite sure where to see the Flask exceptions when editing python files/running the app in docker?
I've tried tailing the api.log
file, but it's only showing valid HTTP requests, and no exceptions.
I've also tried adding logger.info(error)
to the global exception handler in api/core.py
, but that didnt help either.
Any suggestions?
Hi, thanks for your nice work!
When I clone this repo and run docker-compose up --build
, I get the following error:
app | /usr/local/lib/python3.7/site-packages/sqlalchemy/sql/functions.py:67: SAWarning: The GenericFunction 'array_agg' is already registered and is going to be overriden.
app | "is going to be overriden.".format(identifier)
app | /usr/local/lib/python3.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
app | """)
app | Traceback (most recent call last):
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
app | return fn()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 363, in connect
app | return _ConnectionFairy._checkout(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
app | fairy = _ConnectionRecord.checkout(pool)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
app | rec = pool._do_get()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
app | self._dec_overflow()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
app | compat.reraise(exc_type, exc_value, exc_tb)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
app | raise value
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
app | return self._create_connection()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
app | return _ConnectionRecord(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
app | self.__connect(first_connect_check=True)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
app | connection = pool._invoke_creator(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
app | return dialect.connect(*cargs, **cparams)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
app | return self.dbapi.connect(*cargs, **cparams)
app | File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
app | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app | psycopg2.OperationalError: server closed the connection unexpectedly
app | This probably means the server terminated abnormally
app | before or while processing the request.
app |
app |
app | The above exception was the direct cause of the following exception:
app |
app | Traceback (most recent call last):
app | File "manage.py", line 7, in <module>
app | app = create_app()
app | File "/app/api/__init__.py", line 71, in create_app
app | if not database_exists(db_url):
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 472, in database_exists
app | return bool(get_scalar_result(engine, text))
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 455, in get_scalar_result
app | result_proxy = engine.execute(sql)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2168, in execute
app | connection = self._contextual_connect(close_with_result=True)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2229, in _contextual_connect
app | self._wrap_pool_connect(self.pool.connect, None),
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2269, in _wrap_pool_connect
app | e, dialect, self
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1536, in _handle_dbapi_exception_noconnection
app | util.raise_from_cause(sqlalchemy_exception, exc_info)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
app | reraise(type(exception), exception, tb=exc_tb, cause=cause)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
app | raise value.with_traceback(tb)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
app | return fn()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 363, in connect
app | return _ConnectionFairy._checkout(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
app | fairy = _ConnectionRecord.checkout(pool)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
app | rec = pool._do_get()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
app | self._dec_overflow()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
app | compat.reraise(exc_type, exc_value, exc_tb)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
app | raise value
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
app | return self._create_connection()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
app | return _ConnectionRecord(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
app | self.__connect(first_connect_check=True)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
app | connection = pool._invoke_creator(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
app | return dialect.connect(*cargs, **cparams)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
app | return self.dbapi.connect(*cargs, **cparams)
app | File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
app | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
app | This probably means the server terminated abnormally
app | before or while processing the request.
app |
app | (Background on this error at: http://sqlalche.me/e/e3q8)
app | /usr/local/lib/python3.7/site-packages/sqlalchemy/sql/functions.py:67: SAWarning: The GenericFunction 'array_agg' is already registered and is going to be overriden.
app | "is going to be overriden.".format(identifier)
app | /usr/local/lib/python3.7/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
app | """)
app | Traceback (most recent call last):
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
app | return fn()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 363, in connect
app | return _ConnectionFairy._checkout(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
app | fairy = _ConnectionRecord.checkout(pool)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
app | rec = pool._do_get()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
app | self._dec_overflow()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
app | compat.reraise(exc_type, exc_value, exc_tb)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
app | raise value
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
app | return self._create_connection()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
app | return _ConnectionRecord(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
app | self.__connect(first_connect_check=True)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
app | connection = pool._invoke_creator(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
app | return dialect.connect(*cargs, **cparams)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
app | return self.dbapi.connect(*cargs, **cparams)
app | File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
app | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app | psycopg2.OperationalError: server closed the connection unexpectedly
app | This probably means the server terminated abnormally
app | before or while processing the request.
app |
app |
app | The above exception was the direct cause of the following exception:
app |
app | Traceback (most recent call last):
app | File "manage.py", line 7, in <module>
app | app = create_app()
app | File "/app/api/__init__.py", line 71, in create_app
app | if not database_exists(db_url):
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 472, in database_exists
app | return bool(get_scalar_result(engine, text))
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy_utils/functions/database.py", line 455, in get_scalar_result
app | result_proxy = engine.execute(sql)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2168, in execute
app | connection = self._contextual_connect(close_with_result=True)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2229, in _contextual_connect
app | self._wrap_pool_connect(self.pool.connect, None),
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2269, in _wrap_pool_connect
app | e, dialect, self
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1536, in _handle_dbapi_exception_noconnection
app | util.raise_from_cause(sqlalchemy_exception, exc_info)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
app | reraise(type(exception), exception, tb=exc_tb, cause=cause)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
app | raise value.with_traceback(tb)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2265, in _wrap_pool_connect
app | return fn()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 363, in connect
app | return _ConnectionFairy._checkout(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout
app | fairy = _ConnectionRecord.checkout(pool)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 492, in checkout
app | rec = pool._do_get()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get
app | self._dec_overflow()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
app | compat.reraise(exc_type, exc_value, exc_tb)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 153, in reraise
app | raise value
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get
app | return self._create_connection()
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection
app | return _ConnectionRecord(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 437, in __init__
app | self.__connect(first_connect_check=True)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 639, in __connect
app | connection = pool._invoke_creator(self)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
app | return dialect.connect(*cargs, **cparams)
app | File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 453, in connect
app | return self.dbapi.connect(*cargs, **cparams)
app | File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
app | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
app | sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
app | This probably means the server terminated abnormally
app | before or while processing the request.
app |
app | (Background on this error at: http://sqlalche.me/e/e3q8)
Any idea?
Just two quick questions.
Why is line 12 of docker-compose.yml this:
Why is line 58 of api/views/main.py
message=f"Successfully created person {new_person.name} with id: {new_person._id}" and not
message=f"Successfully created person {new_person.name} with id: {new_person.id}"
Is this just a typo? - new_person._id - new_person.id ???
add in common functions and maybe add in continuous integration?
Hello,
Following the Docker setup instructions here:
https://github.com/tko22/flask-boilerplate/wiki/Docker-Setup
docker-compose up -d
This one builds without a problem.
docker ps
shows me everything is running fine.
CONTAINER ID IMAGE COMMAND CREATED STATUS PO
RTS NAMES
c07b67320ea3 flask-boilerplate_app "/app/scripts/docker…" 40 seconds ago Restarting (1) 12 seconds ago
app
add1ae4ccdc3 postgres:10 "docker-entrypoint.s…" 42 seconds ago Up 40 seconds 0.
0.0.0:5432->5432/tcp postgres
docker-compose start
Starting postgres ... done
Starting app ... done
Starts those without any issue.
docker-compose logs app
I quickly get the following error when checking the logs:
app | standard_init_linux.go:211: exec user process caused "no such file or directory"
Any idea if the documentation is out of date? I followed the instructions carefully but it doesn't work.
Thanks!
create a models
folder, then move the Person and Email models into different files. Don't forget the init.py and create db instance!
for easier setup and commands
When running the last command for docker setup docker-compose exec app python manage.py recreate_db
, this error is thrown:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1122, in _do_get
return self._pool.get(wait, self._timeout)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/queue.py", line 145, in get
raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
return fn()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 387, in connect
return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout
rec = pool._do_get()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
self._dec_overflow()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
return self._create_connection()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "postgres" (172.18.0.2) and accepting
TCP/IP connections on port 5432?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 3, in <module>
from api import app
File "/app/api/__init__.py", line 16, in <module>
if not database_exists(db_url):
File "/usr/local/lib/python3.6/site-packages/sqlalchemy_utils/functions/database.py", line 472, in database_exists
return bool(get_scalar_result(engine, text))
File "/usr/local/lib/python3.6/site-packages/sqlalchemy_utils/functions/database.py", line 455, in get_scalar_result
result_proxy = engine.execute(sql)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2063, in execute
connection = self.contextual_connect(close_with_result=True)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2112, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2151, in _wrap_pool_connect
e, dialect, self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1465, in _handle_dbapi_exception_noconnection
exc_info
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2147, in _wrap_pool_connect
return fn()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 387, in connect
return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 766, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 516, in checkout
rec = pool._do_get()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1138, in _do_get
self._dec_overflow()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
raise value
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 1135, in _do_get
return self._create_connection()
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 333, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 461, in __init__
self.__connect(first_connect_check=True)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/pool.py", line 651, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 105, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 393, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "postgres" (172.18.0.2) and accepting
TCP/IP connections on port 5432?
The way I bypassed this was to comment out db.drop_all()
in the recreate_db method within manage.py and rerun the command. I think this error is being thrown due to the db not being initialized, but this needs confirmation.
Using Ubuntu 18.04.
Everything works well until I try to get into the database in the postgres container using the cli.
FATAL: role “postgres” does not exist
This is my model:
class Person(Mixin, db.Model):
"""Person Table."""
__tablename__ = "person"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
faces = db.relationship('Face')
def __repr__(self):
return f"<Person {self.name}>"
This is the route:
from webargs import fields
from webargs.flaskparser import use_args
@main.route("/person", methods=['POST'])
@use_args({
"name": fields.Str(required=True)
})
def post_person(args):
person = Person(**args)
db.session.add(person)
db.session.commit()
# logger.info(f"id: ",person.id)
logger.info(f"Person: {person.to_dict()}")
return create_response(person.to_dict(),201)
Issue: If I execute this code, the logger and the create response will output a null id:
{
"message": "",
"result": {
"_id": null
},
"success": true
}
Expectation: Once we perform the commit, the object properties are available. I've confirmed this by printing/logging the id of the person using person.id
, and it outputs the correct value. to_dict()
outputs null instead of the correct value. It doesn't seem to have the committed object.
Interestingly enough if I uncomment the logger.info
line, or if I add db.session.refresh(), the to_dict()
outputs the correct result.
This is the result when I execute it with the line # logger.info(f"id: ",person.id)
uncommented.
{
"message": "",
"result": {
"_id": 75,
"name": "Test"
},
"success": true
}
the to_dict
function in the class Mixin
is used as a helper function in SQLAlchemy ORM models for converting the objects into python data structures for easy json serialization. However, it isn't recursive.
For example, let's have a Person
and Email
table. Person
has name
and emails
as fields while Email
has total_mail
, email_addr
, person
. The emails
field is a one-to-many relationship to entries in Email table. The following happens when I run to_dict()
function:
person = Person.query.all()[0] # query and get the first person
person.to_dict()
# This outputs
{
name: "tim"
}
# but it doesn't show `emails`, which should be an array of emails even if it exists
# desired output...
{
name: "tim",
emails: [
{
"total_mail": 100,
"email_addr": "[email protected]"
},
{
"total_mail": 20,
"email_addr": "[email protected]"
}
]
}
add typing to add static typing with mypy http://mypy-lang.org/. Type arguments and return values to methods. add to Classes as well
change the return value typing to be the correct type.
get rid of unnecessary dependencies as well
Thanks for this amazing boilerplate, it's simple but enough for a high-quality Flask application. I would just want to make a small suggestion.
Can you add the setting for a WSGI server? Currently, the python manage.py runserver
runs the Flask development server which is not recommended on production environment.
Thanks
Hard coding the urls made it much easier for people to set up but that is a very bad way of storing credentials. Instead, we could use a .ini
file in which we would parse before we set up the flask configs.
this will give future projects extensibility.
include separate documentation too
a lot of applications require scheduled jobs (eg sending emails at certain time, retrieving information, etc). Find a way to optional add https://apscheduler.readthedocs.io/en/latest/ to this boilerplate
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.