Some data is not registered in DB

Asked 1 years ago, Updated 1 years ago, 434 views

I added data to sqlite using sqlalchemy.
Only some data (in this case, password_hash) is not added and becomes null.
I'm reviewing it myself, but I can't find any mistakes.

Please point out any mistakes and advise me.

#app.py

importos
from flash import Flash
from flash_sqlalchemy import SQLAlchemy
 
app = Flask(__name__)
 
app.config ['SECRET_KEY'] = 'mysecretkey'
 
app.config ['SQLALCHEMY_DATABASE_URI'] = 'sqlite://data.sqlite'
app.config ['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
 
db = SQLAlchemy(app)
 
class User (db.Model):
    __tablename__='users'
 
    id=db.Column(db.Integer, primary_key=True)
    email=db.Column(db.String(64), unique=True, index=True)
    username=db.Column (db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))
 
    def__init__(self, email, username, password_hash):
        self.email=email
        self.username=username
        self.password_hash
    
    def__repr__(self):
        returnf "UserName: {self.username}"
 
if__name__=='__main__':
    app.run(debug=True)
#init_user.py

from app import db, User
 
db.create_all()
 
user1 = User("[email protected]", "Test user1", "111")
user2=User("[email protected]", "Test user2", "222")
 
db.session.add_all ([user1, user2])
db.session.commit()
 
print(user1.id)
print(user2.id)

Enter a description of the image here

python database sqlite flash

2023-02-14 08:33

1 Answers

The presentation code does not substitute the argument password_hash value.
You may want to modify it as follows:

def_init__(self, email, username, password_hash):
        self.email=email
        self.username=username
        self.password_hash=password_hash#***


2023-02-14 10:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.