Database with cookiecutter flask

88 views Asked by At

I want to use cookiecutter-flask template to create a local interface for a python app. I want to create a registration system with a database.

You can create a cookiecutter project by running cookiecutter https://github.com/cookiecutter-flask/cookiecutter-flask.git

I edited .env by putting : DATABASE_URL=sqlite:///dev.db

Then I filled the database dev.db by creating a table users using command shell :

sqlite3 dev.db

CREATE TABLE users ( id INTEGER PRIMARY KEY, username VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, created_at TIMESTAMP NOT NULL, first_name VARCHAR(255), last_name VARCHAR(255), active BOOLEAN NOT NULL, is_admin BOOLEAN NOT NULL );

I don't understand why when I want to register a new user, I get sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: users

I tried to create a new database, completely delete the project and make a new one. I also tried to use absolute path but it created a new error sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file

2

There are 2 answers

0
Leos On BEST ANSWER

Problem solved : I had to run in the shell

flask db init
flask db migrate
flask db upgrade
0
Lenntror On
import os
basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'dev.db')

I assume you have a dev.db file in your project folder. Then the correct way to tell flask and sqlite where this file is located is to youse an absolute path as shown above