Add unit test.
Fix an SQL error when where condition is given but neither ids nor usedOnly.