Fix db query bug