flattening database