Python Forum

Full Version: How to create db table with SQLite and SQLAlchemy??
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all I need urgent help I have been stuck on this for days!
I'm trying to create a table in an existing db with SQLAlchemy/SQLite where I have to store a user and password, but it returns an error saying the column pwd doesn't exist!
Am I missing something? Am I messig it up?
I still didn't quite understand, I followed all steps in some online tutorial but nothing still.
Here is the class object that I developed, then from another register form I try to store the pw from the application, but the error should be here in this code:
import os
basedir = os.path.abspath(os.path.dirname(__file__))

from flask import Flask 
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from sqlalchemy import *

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'data-dev.sqlite')
engine= create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
bcrypt = Bcrypt(app)
db = SQLAlchemy(app)

#CREATE TABLE
meta=MetaData()
userstable = Table('users', meta, Column('id', Integer, primary_key = True, autoincrement=True), Column('username', String, unique=True), Column('pwd', String))
meta.create_all(engine)

class User(db.Model):
	#DEFINE COLUMNS TYPE
    id = db.Column(db.Integer(), primary_key=True, autoincrement=True)
    username = db.Column(db.String(64), unique=True)
    pwd = db.Column(db.String(128))

    def __init__(self,username,pwd):
        #self.id=id
        self.username=username
        self.pwd=bcrypt.generate_password_hash(pwd)

THIS IS THE ERROR
sqlalchemy.exc.OperationalError  OperationalError: (sqlite3.OperationalError) table user has no column named pwd [SQL: INSERT INTO user (username, pwd) VALUES (?, ?)] [parameters: ('ita_itf', '$2b$12$VmpTsd0o4uTLj0wGypGu7ujhzYHLlV8k9ekaIP1.yh5lUMMgOM4MC')]
Then I switch to Stackoverflow because on this Forum no replies in 3 issues I have posted. Then you are forced to post it twice. bye