0

I'm building an App and using MariaDB as my database. I have a table "kick_votes". Its primary key consits of three fields:

  • user_id
  • group_id
  • vote_id

I need to delete rows where user_id AND group_id fulfill my conditions or just the vote_id. I enabled the config, that I have to use a key column in my WHERE clause for security issues.

This one is working correctly:

 DELETE FROM kick_votes WHERE (user_id=86 AND group_id=10);
 DELETE FROM kick_votes WHERE vote_id=2;

But I don't want to use two statements, but the following doesn't work:

DELETE FROM kick_votes WHERE (user_id=86 AND group_id=10) OR vote_id=2;

I get the error:

You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.

Why isn't it working?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mcAngular2
  • 299
  • 1
  • 14
  • Possible duplicate of [MySQL error code: 1175 during UPDATE in MySQL Workbench](https://stackoverflow.com/questions/11448068/mysql-error-code-1175-during-update-in-mysql-workbench) – treyBake Nov 30 '18 at 10:48
  • 1
    Can you show us the actual table/primary key definition? – Tim Biegeleisen Nov 30 '18 at 10:49
  • 4
    Possible duplicate of [mysql delete under safe mode](https://stackoverflow.com/questions/21841353/mysql-delete-under-safe-mode) – Tim Biegeleisen Nov 30 '18 at 10:50

2 Answers2

0

Seems like key existence doesn't actually affect the error. From mariadb sources, mysql_delete:

const_cond= (!conds || conds->const_item());
safe_update= MY_TEST(thd->variables.option_bits & OPTION_SAFE_UPDATES);
if (safe_update && const_cond)
{
  my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
             ER_THD(thd, ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
  DBUG_RETURN(TRUE);
}

So the only variant is to turn off safe mode:

set @@sql_safe_updates= 0;
Nikita Malyavin
  • 1,868
  • 1
  • 12
  • 10
0

Try this Kludge:

DELETE FROM kick_votes
    WHERE id > 0    -- assuming this is your PRIMARY KEY
      AND (    (user_id=86 AND group_id=10)
            OR vote_id=2
          );
Rick James
  • 135,179
  • 13
  • 127
  • 222