fix some wrong index keys in neworder transaction

this otherwise causes delivery to fail due to foreign key
inconsistencies.
Switch to optional<T> for Table::select

This causes the code to depend on libc++ and clang with -std=c++1y with
the possibility that this will cause problems.
reformat