Complete the code to define a repository class for accessing user data.
class UserRepository: def __init__(self, db_session): self.db_session = [1]
The repository class stores the database session passed during initialization as db_session to use for data operations.
Complete the method to add a new user object to the database session.
def add_user(self, user): self.db_session.[1](user)
commit instead of add.insert which is not a session method.To add a new object to the session in SQLAlchemy, use the add method.
Fix the error in the method to retrieve a user by ID using the session's query.
def get_user_by_id(self, user_id): return self.db_session.query(User).[1](user_id)
filter_by without calling .first().find.The get method fetches an object by primary key directly, which is efficient and correct here.
Fill both blanks to update a user's email and commit the change.
def update_user_email(self, user_id, new_email): user = self.db_session.query(User).get(user_id) user.email = [1] self.db_session.[2]()
user.email.flush instead of commit.Assign the new email to the user's email attribute, then call commit to save changes.
Fill all three blanks to delete a user by ID and commit the transaction.
def delete_user(self, user_id): user = self.db_session.query(User).[1](user_id) if user: self.db_session.[2](user) self.db_session.[3]()
filter_by without fetching the object.commit before deleting.Retrieve the user by primary key with get, delete the user object, then commit the session.