1

I am using SQLAlchemy and I want to insert a record into the database. I am running my application in Flask-Restful

model.py

class User(BaseModel, db.Model): """Model for the user table""" __tablename__ = 'usertable' email = db.Column(db.String(120), nullable=False) password = db.Column(db.String(200), nullable=False) 

signup.py

from app.models import db, User class Register(Resource): def post(self): email = request.json['email'] password = request.json['password'] db.session.add(User.email = '[email protected]') // Here, I am not sure return email 

I want to add email and password into the table usertable . When I am running the code, error is showing, SyntaxError: keyword can't be an expression . What is the recommended way to add a record in SQLAlchemy ?

1 Answer 1

1

Instead of:

db.session.add(User.email = '[email protected]') 

Try:

user = User(email=email, password=password) db.session.add(user) db.session.commit() 

However, it's not a good idea to store a plain text password in your database. You should modify your User model to store a hashed password instead.

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

1 Comment

I think you can assume the password is already hashed client-side, but I agree its a good practice nontheless

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.