Jake Jackson
Jake Jackson

Reputation: 1245

Flask-SQLalchemy error saying my table has no column named x

I am just trying to create a simple database, add a word, print it, then drop it, but I am getting an error message that I can't make any sense out of.

Error message:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table post has no column named word
[SQL: INSERT INTO post (word) VALUES (?)]
[parameters: ('Cheese 0',)]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    word = db.Column(db.String(10))


def create_db():
    db.create_all()

    for x in range(5):
        p = Post(word='Cheese ' + str(x))
        db.session.add(p)
        db.session.commit()

The provided link. I am new to Flask and SQLalchemy; I can't quite make out what the information is trying to say to me.

Upvotes: 0

Views: 1485

Answers (1)

Pouya Esmaeili
Pouya Esmaeili

Reputation: 1261

You missed db.session.commit()

def create_db():
    db.create_all()
    db.session.commit()

    for x in range(5):
        p = Post(word= f'Cheese {x}')
        db.session.add(p)
        db.session.commit()

Upvotes: 1

Related Questions