python - Creating database with SQLAlchemy in Flask -
i'd know, need create sqlalchemy database in flask. according documentation should create model in flask app , go python shell , create using db.create_all(). doesn't work.
my flask app:
import os import flask import settings flask_sqlalchemy import sqlalchemy app = flask.flask(__name__) app.config['session_type'] = 'filesystem' app.secret_key = os.urandom(24) app.config['sqlalchemy_database_uri'] = 'sqlite:////database.db' db = sqlalchemy(app) (...)
model:
from app import db class user(db.model): id = db.column(db.integer, primary_key = true) username = db.column(db.string(15), unique = true) password = db.column(db.string(15), unique = true) tasks = db.relationship('task', backref='author', lazy='dynamic') def __init__(self, username, password): self.username = username self.password = password class task(db.model): id = db.column(db.integer, primary_key = true) scene = db.column(db.string(140), nullable = false) state = db.column(db.integer(1), nullable = false) progress = db.column(db.integer(3), nullable = false) add_date = db.column(db.datetime, nullable = false) start_date = db.column(db.datetime, nullable = false) finish_date = db.column(db.datetime, nullable = false) rendered_scene = db.column(db.string(140)) user_id = db.column(db.integer, db.foreignkey('user.id'))
error code:
>>> app import db >>> db.create_all() traceback (most recent call last): file "<stdin>", line 1, in <module> file "c:\python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 895, in create_all self._execute_for_all_tables(app, bind, 'create_all') file "c:\python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 887, in _execute_for_all_tables op(bind=self.get_engine(app, bind), **extra) file "c:\python27\lib\site-packages\sqlalchemy\sql\schema.py", line 3420, in create_all tables=tables) file "c:\python27\lib\site-packages\sqlalchemy\engine\base.py", line 1727, in_run_visitor self._optional_conn_ctx_manager(connection) conn: file "c:\python27\lib\contextlib.py", line 17, in __enter__ return self.gen.next() file "c:\python27\lib\site-packages\sqlalchemy\engine\base.py", line 1720, in_optional_conn_ctx_manager self.contextual_connect() conn: file "c:\python27\lib\site-packages\sqlalchemy\engine\base.py", line 1910, incontextual_connect self.pool.connect(), file "c:\python27\lib\site-packages\sqlalchemy\pool.py", line 338, in connect return _connectionfairy._checkout(self) file "c:\python27\lib\site-packages\sqlalchemy\pool.py", line 645, in _checkout fairy = _connectionrecord.checkout(pool) file "c:\python27\lib\site-packages\sqlalchemy\pool.py", line 440, in checkout rec = pool._do_get() file "c:\python27\lib\site-packages\sqlalchemy\pool.py", line 1058, in _do_get return self._create_connection() file "c:\python27\lib\site-packages\sqlalchemy\pool.py", line 285, in _create_connection return _connectionrecord(self) file "c:\python27\lib\site-packages\sqlalchemy\pool.py", line 411, in __init__ self.connection = self.__connect() file "c:\python27\lib\site-packages\sqlalchemy\pool.py", line 539, in __connect connection = self.__pool._creator() file "c:\python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 96, in connect connection_invalidated=invalidated file "c:\python27\lib\site-packages\sqlalchemy\util\compat.py", line 199, in raise_from_cause reraise(type(exception), exception, tb=exc_tb) file "c:\python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 90, in connect return dialect.connect(*cargs, **cparams) file "c:\python27\lib\site-packages\sqlalchemy\engine\default.py", line 377, in connect return self.dbapi.connect(*cargs, **cparams) sqlalchemy.exc.operationalerror: (operationalerror) unable open database file none none
you have 1 many /
in database uri. format dialect+driver://user:pass@host:port/db_name
. sqlite, db_name
path database. you've specified absolute path /database.db
, means you're trying create database in filesystem's root directory.
using sqlite:///database.db
(a relative path) create database in (relative to) current working directory.
you want specify absolute path, build based on project location, since run application folder. assuming want create db in same directory app setup code, build path relative __file__
.
db_path = os.path.join(os.path.dirname(__file__), 'app.db') db_uri = 'sqlite:///{}'.format(db_path) app.config['sqlalchemy_database_uri'] = db_uri
Comments
Post a Comment