1

Context: I'm creating my first flask web app as a twitter clone in which developers can post "dev logs" to a game they're developing.

From what I've learned in this tutorial, I've repackaged my app to become four python modules. I'm still a bit confused and I think I got some things wrong, but when I run the program through the script below:

from home import app

if __name__ == "__main__":
    app.run(debug=True)

Where home is the name of my flask app. Running this outputs this error:

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from home import app
  File "/Users/AdityaD/iCloud Drive (Archive)/Desktop/DevlogIO/home/__init__.py", line 11, in <module>
    from home import routes
  File "/Users/AdityaD/iCloud Drive (Archive)/Desktop/DevlogIO/home/routes.py", line 10, in <module>
    db.session.commit()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 943, in commit
    self.transaction.commit()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 467, in commit
    self._prepare_impl()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 447, in _prepare_impl
    self.session.flush()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2254, in flush
    self._flush(objects)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2381, in _flush
    transaction.rollback(_capture_exception=True)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 249, in reraise
    raise value
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2345, in _flush
    flush_context.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 395, in execute
    rec.execute(self)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 560, in execute
    uow
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 181, in save_obj
    mapper, table, insert)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 866, in _emit_insert_statements
    execute(statement, params)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 948, in execute
    return meth(self, multiparams, params)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
    context)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 248, in reraise
    raise value.with_traceback(tb)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user [SQL: 'INSERT INTO user (username, password) VALUES (?, ?)'] [parameters: ('admin', 'admin123')] (Background on this error at: http://sqlalche.me/e/e3q8)

This is my init file:

from flask import Flask
from flask_session import Session

# Set's up the app with the given name 
app = Flask(__name__) 
# Secret key for sessions
app.config['SECRET_KEY'] = 'eb02cfb5079a2b6bdeb8bddb69ca937b' 
# Sets up config for database with SQLite, which is easy to set up. 
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'

from home import routes
from home.models import *

db.create_all()

Below is routes.py:

from flask import render_template, url_for, flash
import os, datetime
from home import app
from home.models import db

#dummy data
import home.models
theUser = home.models.User(username='admin', password='admin123')
db.session.add(theUser)
db.session.commit()
admin = home.models.User.query.filter_by(username='admin').first()
zombies = home.models.Games(game='Shoot Zombies!', user_id=admin.id)
ball = home.models.Games(game='Bounce & Ball', user_id=admin.id)
db.session.add(zombies)
db.session.add(ball)
db.session.commit()

def getCurrentUser():
    return admin

from home import posts

@app.route("/", methods=['GET', 'POST'])
def index():
    form = posts.WriteLog()
    if form.validate_on_submit():
        username = current_user.username
        post = form.devLog.data
        game = Games.query.filter_by(game=form.gameSelect.data).first().id
        title = form.title.date
        newPost = Post(title=title, game_id=game, content=post)
        return render_template("index.html", form=form, data=Post)
    return render_template("index.html", form=form, data=Post)

Below is models.py:

from home import app
from flask_sqlalchemy import SQLAlchemy 
import os, datetime

# Sets up SQL Database
db = SQLAlchemy(app)

### Database Stuff ###
# Creates a table for the user

class User(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), nullable=False)
    password = db.Column(db.String(20), nullable=False)
    games = db.relationship('Games', backref="user", lazy=True)

    def __repr__(self):
        return f"User('{self.username}', '{self.password}', '{self.games}')"

# Creates a table for games
class Games(db.Model):
    __tablename__ = "game"
    id = db.Column(db.Integer, primary_key=True)
    game = db.Column(db.String(40), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) 
    logs = db.relationship('Post', backref="game", lazy=True)

    def __repr__(self):
        return f"Game('{self.game}', '{self.logs}')"

class Post(db.Model):
    __tablename__ = "post"
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
    game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False)
    content = db.Column(db.Text, nullable=False)

    def __repr__(self):
        return f"Post('{self.title}', '{self.date_posted}', '{self.game_id}', '{self.content}')"

And posts.py is just an app which manages the forms. As I understand it, the User table was not created. However, I have run the db.create_all() command in the init file. Help would be appreciated!

UPDATE: Now I have a new bug.

sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 140734986048960 and this is thread id 123145577107456 [SQL: 'SELECT post.id AS post_id, post.title AS post_title, post.date_posted AS post_date_posted, post.game_id AS post_game_id, post.content AS post_content \nFROM post \nWHERE ? = post.game_id'] [parameters: [{'%(4536059552 param)s': 1}]] (Background on this error at: http://sqlalche.me/e/f405)

For reference, this is my index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>DevlogIO</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
    <div id="container">
        <h1>Devlog IO</h1>
        <h2>Home</h2>
        <p>See the progress of your favorite developers, and discover new and rising game developers!</p>
        <div>
            <form action="" method="POST">
                {{ form.hidden_tag() }}
                <fieldset>
                    <legend>What was your progress for today?</legend>
                    <div>
                        {{ form.gameSelect(class="") }}
                    </div>
                    <br>
                    <div>
                        {{ form.title.label(class="") }}
                        {{ form.title(class="")  }}
                    </div>
                    <br>
                    <div>
                        {{ form.devLog.label(class="") }}
                        {{ form.devLog(class="")  }}
                    </div>
                    <br>
                    <div>
                        {{ form.submit(class="") }}
                    </div>  
                    <br>  
                </fieldset>
            </form>
        </div>
        <hr>
        <div id="stream">
                {% for post in data.query.all() %}
                    <fieldset>
                        <p><strong>{{ post.title }}, {{ form.gameSelect.data }}</strong></p>
                        <p><i>by {{ post.game_id }}</i></p>
                        <p><i>{{ post.date_posted }}</i></p>
                        <p>{{ post.content }}</p>    
                    </fieldset>
                    <br>
                {% endfor %}
        </div>

    </div>

    <script src="{{ url_for('static', filename='script.js') }}"></script>
</body>
</html>

1 Answer 1

1

in traceback in first line is said that error appears on line 11: from home import routes Your app even didn't get to db.create_all() and fails on import in which your references to db object that does ot exist yet. Try to put db.create_all() into routes.py

Sign up to request clarification or add additional context in comments.

1 Comment

This seemed to do it.However, I now have an entirely new bug: sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 140734986048960 and this is thread id 123145577107456 [SQL: 'SELECT post.id AS post_id, post.title AS post_title, post.date_posted AS post_date_posted, post.game_id AS post_game_id, post.content AS post_content \nFROM post \nWHERE ? = post.game_id'] [parameters: [{'%(4536059552 param)s': 1}]] (Background on this error at: sqlalche.me/e/f405)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.