[#74] Assign generated id if none is provided. Improve query generation.