Change SQL from raw sqlite3 to orm