'Unable to update database Flask-SQLAlchemy Model
I have a challenge updating table fields in the database using Flask-SQLAlchemy models. When I call the update endpoint, it does not update the database I have a small model for user as follows
User Model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(80), nullable=False)
last_name = db.Column(db.String(80), nullable=False)
password = db.Column(db.String(120), nullable=False)
def save_to_db(self):
db.session.add(self)
db.session.commit()
@classmethod
def update(cls):
db.session.commit()
@classmethod
def find_by_id(cls, id)
return cls.query.filter_by(id=id).first()
User Resource
class UserResource(Resource):
@jwt_required
def put(self, user_id=None):
user = User.find_by_id(id=user_id)
if not user:
return { 'message': 'User not found' }
user.first_name = 'foo'
user.last_name = 'bar'
user.update()
return { 'message': 'user updated' }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
