Search code examples
pythonflaskcassandracql

CQL update query not working using Python


I am trying to update new password after reset to cassandra db. This is the query I have written where both username and password fields are dynamic. Is this right?

    def update_db(uname, pwd):
        query = session.prepare('update user.userdetails set "password"=%s where "username" = ? ALLOW FILTERING', pwd)
        session.execute(query, (uname,))

update_db(username, new_pwd)

I am calling this through an API. But it doesn't seem to update.


Solution

  • Alex is absolutely correct in that you need to provide the complete PRIMARY KEY for any write operation. Remove ALLOW FILTERING and your query should work as long as your primary key definition is: PRIMARY KEY (username).

    Additionally, it's best practice to parameterize your entire prepared statement, instead of relying on string formatting for password.

        query = session.prepare('update user.userdetails set "password"=? where "username"=?')
        session.execute(query,[pwd,uname])
    

    Note: If at any point you find yourself needing the ALLOW FILTERING directive, you're doing it wrong.