codeclue
codeclue

Reputation: 237

INSERT a record into a table in SQLAlchemy

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 ?

Upvotes: 1

Views: 1714

Answers (1)

Greg Cowell
Greg Cowell

Reputation: 693

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.

Upvotes: 1

Related Questions