ilanos
ilanos

Reputation: 193

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.

Upvotes: 1

Views: 411

Answers (2)

Aaron
Aaron

Reputation: 57748

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.

Upvotes: 2

Alex Ott
Alex Ott

Reputation: 87109

for updating record you need to provide primary key(s) completely. It will not work with ALLOW FILTERING - you need first to get all primary keys that you want to update, and then issue individual update commands. See the documentation for more detailed description of UPDATE command.

If you really want to specify the default value for some column - why not simply handle it with something like .get('column', 'default-value')?

Upvotes: 1

Related Questions