Prevent duplicate attachment orderings

Add not null and a unique index on ordering to prevent duplicate
attachment orderings.